How does vue save the user name?

A background management system. After the user logs in, the upper right corner of the page displays "Welcome, admin"

admin is the account logged in

when login is successful, I will save token in cookie. Part of the code is as follows

setCookie("token",this.token,3600*12); //
this.$router.push("/home") //

but where should I save the user name? If it is refreshed in vuex, it will be gone

.

is there any cookie that can store both user name and token at the same time?

how do people usually solve this situation?

Mar.07,2021

refresh


if you want to refresh the browser without losing data, you can use cookie, localStorage, sessionStorage, and refresh to re-obtain the user name and token, in


cookie. Because each request browser will bring the data in cookie to the server, in general, only the data exchanged between the front and back ends is saved in cookie, which is recommended to be saved in sessionStorage or localStorage. You can take a look at my article https://codeshelper.com/a/11.


const store = new Vuex.Store({
  // 
  state: {
    username: JSON.parse(sessionStorage.getItem('username')) || ""
  },
  mutations: {
    userName(state, msg){
      sessionStorage.setItem('username', JSON.stringify(msg))
      state.username= msg;
    }
  }
})

in short, you need to have a back-end interface to support

your this should be single page app bar

first of all, after a successful login, the backend login interface is generally requested, such as calling login, to return a token after success, and then you store it in the cookie, which you will bring every time.

the backend interface to be verified.
Menu