[Vuejs]-Nuxt.js middleware redirect based on role

-1👍

Seems middleware didn’t redirect me after login, so I had to use the router and redirect based on the role after the login has completed.

EDIT: As asked I’m adding also the code solution.

In my login function, after logging in I check the role of the user (which in my case was saved in the JWT token).

login(data) {
      const self = this
      this.$axios.post('/auth/login', data).then(function(response) {
        const tkData = self.parseJwt(response.data.Token)
        const auth = {
          accessToken: response.data.Token,
          email: tkData.email,
          role: tkData.role
        }
        self.$store.commit('setAuth', auth)
        Cookie.set('auth', auth)
        if (tkData.role === 'admin') {
          self.$router.push('/admin')
        } else {
          self.$router.push('/')
        }
      })
    }

PS: You can check out my code and test it in my GitHub

Leave a comment