Vue login intercept

after saving token, how to click the browser return button without going back to the login page

Mar.20,2021

just add one more judgment according to the code you posted. If you get the token, you can determine whether the destination route is / login ;

if (!getToken()) {
    ...
} else {
    if (to.path === '/login') {
        next({
            path: from.path
        })
    } else {
        ...
    }
}

in addition, when you intercept the request uniformly, you can add the status code to determine whether you have logged in

.

you don't have to grab every router change in main.js, you can solve the problem by processing it on the login page.
add:

to the login page
methods: {},
beforeRouteEnter (to, from, next) {
    if (!getToken()) { // cookie
       next()
    } else {
       next({
           path:"/" 
       })
    }
}
Menu