简体   繁体   中英

How to create redirect after auth user, vue.js?

I got token and it thing stored in vue state and local.storage. How to create redirect after auth and after get token?

At the moment I have this code, Its redirect, but after get token redirect isn't happens and without check token.

I need to check token and redirect to fullPath, pls help!

var Auth = {
  loggedIn: false,
  login: function () { this.loggedIn = true },
  logout: function () { this.loggedIn = false }
}
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth) && !Auth.loggedIn) {
    next({
      path: '/login',
      query: {redirect: '/'}
    })
  } else {
    next()
  }
})

In general it should work like that:

  • user enters login/pass
  • vuex action userLogin fired
  • userLogin action sends data to server, server returns token
  • userLogin action saves token to the localStorage
  • userLogin commits mutation USER_LOGIN with token and/or user credentials

Then to make auto-login with saved token (after browser closing, etc)

  • on the app start you should call userLoginLocal vuex action
  • userLoginLocal reads token from localStorage
  • userLoginLocal should validate token or/and send to server for validation
  • userLoginLocal commits mutation USER_LOGIN with token and/or user credentials (if token is valid)

NOTES: Mutation USER_LOGIN makes isLoggedIn = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM