npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

myhome-nuxt-token-auth-module

v2.4.5

Published

Token based authentication module for Nuxt.js

Downloads

8

Readme

Nuxt.js token auth module

Token based authentication module for Nuxt.js apps.

Setup

Install with npm:

npm install nuxt-token-auth-module @nuxtjs/axios

Edit nuxt.config.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:

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.

export default {
  tokenAuth: false
}

Settings

Example:

{
  // 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

this.$tokenAuth.login([data])
// return Promise object with axios request
// [data] - data object for axios request

logout

this.$tokenAuth.logout([data])
// return Promise object with axios request
// [data] - data object for axios request

refresh

this.$tokenAuth.refresh(token, refreshToken)
// return Promise object with axios request

getToken

this.$tokenAuth.getToken()
// return auth token in 'spa' mode

this.$tokenAuth.getToken(context)
// return auth token in 'universal' mode

getRefreshToken

this.$tokenAuth.getRefreshToken()
// return refresh token in 'spa' mode

this.$tokenAuth.getRefreshToken(context)
// return refresh token in 'universal' mode 

setToken

this.$tokenAuth.setToken(token)
// setting auth token in 'spa' mode

this.$tokenAuth.setToken(token, context)
// setting auth token in 'universal' mode

setRefreshToken

this.$tokenAuth.setRefreshToken(refreshToken)
// setting refresh token in 'spa' mode

this.$tokenAuth.setRefreshToken(refreshToken, context)
// setting refresh token in 'universal' mode

removeToken

this.$tokenAuth.removeToken()
// remove token in 'spa' mode

this.$tokenAuth.removeToken(context)
// remove token in 'universal' mode 

removeRefreshToken

this.$tokenAuth.removeRefreshToken()
// remove refresh token in 'spa' mode

this.$tokenAuth.removeRefreshToken(context)
// remove refresh token in 'universal' mode 

Usage example

Login

<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

<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