How to modify every refresh when exiting to the login page?

For the project of

vue, the nuxt, only adds this line of code to the middleware, and each refresh will exit to the login page. Does it have anything to do with this code? what is the general problem?

export default function ({ store, redirect }) {
  if (!store.state.user.token || !sessionStorage.token) {
    return redirect("/users/login");
  }
  return true;
}
Jun.18,2021

refresh the Vue instance and store are destroyed, and then initialized again. Store certainly won't have any information about the user. If you want to save user information after refreshing, it is recommended that cookie or localstorage, generally use cookie, to set the expiration time for users who save their login status within a period of time, otherwise you need to save an extra timestamp field to determine whether it expires each time you read it.


refresh the page, sessionStorage will not disappear. See if it is your logic to initialize the page with operations related to emptying the values in sessionStorage during refresh

browser window closes before sessionStorage disappears

store cannot persist storage, so store will be emptied every time you refresh. If you have to use sessionStorage, please check whether there is an operation to empty sessionStorage in the code. If not, it is recommended to use localStorage for persistent storage.

finally, I hope my answer can help you. If it is useful to you, please accept it. Welcome to follow my Wechat official account Front end Guide

store refresh does not exist. sessionStorage.token is not the way to read sessionStorage .
in addition, sessionStorage closes tab will disappear. I don't know if this is your requirement (usually use localStorage ).


sessionStorage.getItem ('token')

Menu