0

How to persist current user data in VueJS app? Right now I create in my router an beforeEnter and fetch current user data from server, but this solution make very slow my app because i need launch this request before every page...

beforeEnter: (to, from, next) => {
    UserService.getUserDate().then(response => {
        store.auth.user = response.data;
        next();
    })
}

I think about after first login to system, fetch data of the user and persist it in localStorage, but it is save?

thanks for any help

markWanka
  • 704
  • 1
  • 12
  • 29
  • You listed Vuex as a keyword to your question, so apparently you have heard of it. I recommend storing your user credentials in Vuex after they log in. I have used it for a token auth scheme and it works well. – Tim Mar 11 '21 at 21:22
  • @Tim, ok but after refresh page, all of my data from vuex store is cleaned, so I must launch another request to have this data – markWanka Mar 12 '21 at 10:20
  • You fetch your userData when app is loaded. Preferably in a layout tier component. Then you store the userData in both localStorage and vuex. In the beforeEnter middleware you can use the value from vuex. When page is refreshed you can use the value from localstorage to populate your userInfo. The process is already explained in this question https://stackoverflow.com/questions/42603909/accessing-vuex-state-when-defining-vue-router-routes – Risalat Zaman Mar 15 '21 at 12:19

0 Answers0