vue-easy-jwt

Library for decoding a jwt token
5,094 4
Install
npm install vue-easy-jwt
Latest Version:2.0.6
License:MIT
Last Updated:Nov 17, 2022
Links: GitHub  ·  npm
Maintainer: gustavo0197

This is a small library for decoding a json web token in vue. Since the header and payload is base64 encoded you can easily know the stored data with no password, you can also know if the token is expired or not.

NPM

You can find a version for React here

Version 2.0

In this version you can use this library as a Vue plugin and you'll be able to use it in your components using this.$jwt

To install this module you can use yarn or npm
yarn add vue-easy-jwt

or

npm install vue-easy-jwt
You can use it in a vue project
main.js
import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import VueEasyJwt from 'vue-easy-jwt'

Vue.config.productionTip = false
// You can access to the plugin in your components
// by using this.$jwt
Vue.use(VueEasyJwt)

or
// You can also define a function to get your token in an easy way.
// In your components you should do this:   this.$jwt.getToken();
Vue.use(VueEasyJwt, {
  getToken: () => localStorage.getItem("authorization")
})

new Vue({
  render: h => h(App)
}).$mount('#app')

In your components
<script>
  export default {
    data() {
      return {
        // If you defined a function to get your token
        // you can use this and you'll get the token
        yourToken: this.$jwt.getToken(),
      };
    },
    methods: {
      decodeToken() {
        // Decode some token
        const decodedToken = this.$jwt.decodeToken(this.yourToken);
        // You should get a json if your token has a
        // valid format, even if it's expired.
        // And you will get null if your token
        // has an invalid jwt format, null or undefined.
        /*  
          { name: 'Gustavo', iat: 1596408259, exp: 4752168259 } 
        */
      },
      expiredToken() {
        // you will get a true / false response
        // true  -> if the token is already expired
        // false -> if the token is not expired
        const isExpired = this.$jwt.isExpired(this.yourToken);
      },
    },
  };
</script>
You can also use it for navigation guards
import VueRouter from "vue-router";
import { VueEasyJwt } from "vue-easy-jwt";
const jwt = new VueEasyJwt();

// import your components
import SignIn from "path_to_component/SignIn.vue";
import Home from "path_to_component/Home.vue";

const routes = [
  {
    path: "/signin",
    component: SignIn,
  },
  {
    path: "/home",
    component: Home,
    meta: {
      requiresAuth: true,
    },
  },
];

const router = new VueRouter({ routes });

router.beforeEach((to, from, next) => {
  to.matched.some((route) => {
    if (route.meta.requiresAuth) {
      // import your token
      const yourToken = localStorage.getItem("token");

      // returns true if is expired
      // if it is an empty string, null or undefined
      // will return true (expired)
      if (jwt.isExpired(yourToken)) {
        // if is expired the user should sign in again
        next({ path: "/signin" });
      } else {
        next();
      }
    } else {
      next();
    }
  });
});

export default router;

Related Packages

vue-auth

Vue plugin for easily managing auth state

1,200 15
vue-jwt-auth

Vue.js JWT Auth plugin.

1,057 2,324
@bordman1/vue-auth

Vue.js token based authentication plugin. Supports simple token based and Json W...

962 2,324
vue-auth-jwt

*An open-source Vue.js authentication package for JWT*

400
web3-token

Web3 Token is a new way to authenticate users in hybrid dApps using signed messa...

81,222 368

Version History

Version Released License
2.0.6 MIT
2.0.5 MIT
2.0.3 MIT
2.0.2 MIT
2.0.1 MIT
Pre-releases (7)
Version Released License
2.0.0-beta.6 MIT
2.0.0-beta.5 MIT
2.0.0-beta.4 MIT
2.0.0-beta.3 MIT
2.0.0-beta.2 MIT