# nuxt-token-auth-module

> Token based authentication module for Nuxt.js

Latest version **2.4.3** (published 2021-07-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install nuxt-token-auth-module
pnpm add nuxt-token-auth-module
yarn add nuxt-token-auth-module
bun add nuxt-token-auth-module
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.4.3 |
| Published | 2021-07-06 |
| First published | 2020-03-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | yodaskillme@gmail.com |
| Maintainers | egorvdovenko |
| Keywords | nuxtjs, token-auth |

## Links

- npm: https://www.npmjs.com/package/nuxt-token-auth-module
- Repository: https://github.com/egorvdovenko/token-auth-module
- Homepage: https://github.com/egorvdovenko/token-auth-module#readme
- Issues: https://github.com/egorvdovenko/token-auth-module/issues
- npm.io page: https://npm.io/package/nuxt-token-auth-module

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 2.4.3 (latest) — 2021-07-06
- 2.4.2 — 2021-06-29
- 2.4.1 — 2021-06-15
- 2.4.0 — 2021-05-14
- 2.3.0 — 2021-02-26
- 2.2.3 — 2020-12-22
- 2.2.2 — 2020-09-29
- 2.2.1 — 2020-09-29
- 2.2.0 — 2020-09-14
- 2.1.4 — 2020-07-28
- 2.1.3 — 2020-07-28
- 2.1.2 — 2020-07-27
- 2.1.1 — 2020-07-27
- 2.1.0 — 2020-07-27
- 2.0.0 — 2020-07-26
- … 5 more at https://npm.io/package/nuxt-token-auth-module/versions

## README

# Nuxt.js token auth module

Token based authentication module for Nuxt.js apps.

## Setup

Install with npm:
```bash
npm install nuxt-token-auth-module @nuxtjs/axios
```

Edit `nuxt.config.js`:
```js
modules: [
  // Modules connection order matters
  'nuxt-token-auth-module',
  '@nuxtjs/axios'
],
tokenAuth: {
  // Settings
}
```

### Important

When adding auth-module to a new Nuxt project ensure you have activated the Vuex store.

## Middleware

You can enable `tokenAuth` middleware. When this middleware is enabled on a route and `loggedIn` is `false` user will be redirected to `redirects.login` route.

Edit `nuxt.config.js`:
```js
router: {
  middleware: ['tokenAuth']
}
```

In case of global usage, You can set `tokenAuth` option to `false` in a specific component and the middleware will ignore that route.

```js
export default {
  tokenAuth: false
}
```

## Settings

Example:
```js
{
  // Each endpoint is a required option.
  // It will be used to make requests using axios.
  endpoints: {
    refresh: {
      url: 'http://localhost:3000/api/refresh',
      method: 'post'
    },
    login: {
      url: 'http://localhost:3000/api/login',
      method: 'post'
    },
    logout: {
      url: 'http://localhost:3000/api/logout',
      method: 'post'
    }
  },
  // Each redirect is a required option.
  // It will be used to make redirect, after failed loggedIn check or refresh request.
  redirects: {
    login: '/login'
  },
  // It will be used to set cookie options.
  cookie: {
    // Default path is '/'.
    path: '',
    // Default domain is current.
    domain: '',
  }
}
```

## Methods

Anywhere in your application you can use following methods:

### login

```js
this.$tokenAuth.login([data])
// return Promise object with axios request
// [data] - data object for axios request
```

### logout

```js
this.$tokenAuth.logout([data])
// return Promise object with axios request
// [data] - data object for axios request
```

### refresh

```js
this.$tokenAuth.refresh(token, refreshToken)
// return Promise object with axios request
```

### getToken

```js
this.$tokenAuth.getToken()
// return auth token in 'spa' mode

this.$tokenAuth.getToken(context)
// return auth token in 'universal' mode
```

### getRefreshToken

```js
this.$tokenAuth.getRefreshToken()
// return refresh token in 'spa' mode

this.$tokenAuth.getRefreshToken(context)
// return refresh token in 'universal' mode 
```

### setToken

```js
this.$tokenAuth.setToken(token)
// setting auth token in 'spa' mode

this.$tokenAuth.setToken(token, context)
// setting auth token in 'universal' mode
```

### setRefreshToken

```js
this.$tokenAuth.setRefreshToken(refreshToken)
// setting refresh token in 'spa' mode

this.$tokenAuth.setRefreshToken(refreshToken, context)
// setting refresh token in 'universal' mode
```

### removeToken

```js
this.$tokenAuth.removeToken()
// remove token in 'spa' mode

this.$tokenAuth.removeToken(context)
// remove token in 'universal' mode 
```

### removeRefreshToken

```js
this.$tokenAuth.removeRefreshToken()
// remove refresh token in 'spa' mode

this.$tokenAuth.removeRefreshToken(context)
// remove refresh token in 'universal' mode 
```

## Usage example

### Login

```js
<script>
export default {
  data () {
    return {
      login: 'login',
      password: 'password'
    }
  },
  methods: {
    onLoginSubmit () {
      this.$tokenAuth
        .login({
          data: {
            login: this.login,
            password: this.password
          }
        })
        .then(() => {
          this.$router.push({
            name: 'index'
          })
        })
        .catch((error) => {
          console.log('login.vue => onLoginSubmit() => error: ', error)
        })
    }
  }
}
</script>
```

After you log in, `token` will be updated automatically if any of the requests receives a `401 Unathorized` status code and `refreshToken` is not expired.
If the lifetime of both tokens has expired, then you will be redirected to the `login` route from settings `redirect` section.

### Logout

```js
<script>
export default {
  methods: {
    onLogoutSubmit () {
      this.$tokenAuth
        .logout()
        .then(() => {
          this.$router.push({
            name: 'login'
          })
        })
        .catch((error) => {
          console.log('index.vue => onLogoutSubmit() => error: ', error)
        })
    }
  }
}
</script>
```

## License

[ISC](https://opensource.org/licenses/ISC)

---
_Source: https://npm.io/package/nuxt-token-auth-module · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
