I'm building a single page application where users can see different pages depending if they've logged in or not. The login call works perfectly and an authorization token is saved in the Local Storage.
The setup: I've set up a getter called loggedIn which returns true if a token is set on the state. This is the exact code in my auth.js module of the store:
const state = {
accessToken: TokenService.getToken(),
}
const getters = {
loggedIn: (state) => {
return state.accessToken ? true : false
},
The TokenService has this:
getToken() {
return localStorage.getItem(TOKEN_KEY)
}
The problem: when the user logs in, the token is correctly saved in the Local Storage (I can see it). However, the Vuex panel in DevTools shows the getter loggedIn as false and the accessToken as undefined. The problem fixes itself when I manually refresh the page. The store is then properly populated.
What am I doing wrong? I need the store to be reactive so I can show some pages or others depending on the loggedIn getter.