Vue Router: prevent user from accessing login route if already logged in

Asked

Viewed 1,552 times

9

I am configuring the routes of my application in Vue.js, initially this all OK, after logging in the user is redirected to the dashboard, now my question is how can I prevent the user from accessing the route "/login" (again) after he already logged in and redirected him back to the previous route? (which he was before he typed in the address bar "/login")

Reading the documentation of , I know I can use the navigation Guards, and using the global router.beforeEach, which has three parameters: to, from and next, I know I can manipulate this behavior using the from to return the user to the previous route if they access the "/login" while logged in, but I still haven’t been able to get him redirected.

Here’s my example of code: router/beforeEach.js

import { checkUserToken } from 'src/utils/storageData'

const isAuthRoute = route => route.path.indexOf('/login') !== -1

const isLogged = () => !checkUserToken()

const needAuth = auth => auth === true

const beforeEach = (to, from, next) => {
  const auth = to.meta.requiresAuth

  // verifica se a rota requer autenticação
  if (needAuth(auth)) {
    // verifica se o usuário já está autenticado
    if (!isAuthRoute(to) && !isLogged()) {
      next('/login')
    } else {
      if (isLogged() && to.path === '/login') {
        from()
      }
      next()
    }
  } else {
    next()
  }
}

export default beforeEach

router/index.js:

import Vue from 'vue'

import VueRouter from 'vue-router'

import {routes} from './routes'

import beforeEach from './beforeEach'

Vue.use(VueRouter)

const router = Vue.router = new VueRouter({
  linkActiveClass: 'active',
  saveScrollPosition: true,
  mode: 'history',
  base: __dirname,
  routes
})

// pega as configurações do import acima
router.beforeEach(beforeEach)

export default router
  • I haven’t done much with the Router yet, but from the looks of it it’s all OK, try logging in isLogged() and to.path to see if you are getting to this part of the code and if the values are hitting to see if you can find something.

  • Are you sure from is a function? I think you have to use next(from) instead of from()

2 answers

1

On the '/login' route beforeCreate can be defined by calling a goback() method, see https://vuejs.org/v2/api/#beforeCreate

beforeCreate () {
    if (isLogged()) {
      this.goBack()
    }
},
methods: {
    goBack () {
      window.history.length > 1
        ? this.$router.go(-1)
        : this.$router.push('/')
    }
  }

0

well, one solution I used is the following, I did a check to see if the user is logged in and if the route is "/login", then I gave a next('/') to home page. and that if gets out of the first if.

if (isLogged() && to.path === '/login') {
    next('/')
 }
  • I have a post about a plugin I made for this purpose: http://www.vuejs-brasil.com.br/acl-vue/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.