I'm using Vuex and Laravel sanctum to authenticate users. The following is my Vuex auth.js
import axios from 'axios'
export default {
namespaced: true,
state:{
authenticated: false,
user: null
},
getters:{
authenticated (state) {
return state.authenticated
},
user (state) {
return state.user
}
},
mutations:{
SET_AUTHENTICATED(state, value){
state.authenticated = value
},
SET_USER(state, data){
state.user = data
}
},
actions:{
async login ({commit}, credentials){
await axios.get('/sanctum/csrf-cookie')
await axios.post('/api/login', credentials).then(response => {
commit('SET_AUTHENTICATED', true)
commit('SET_USER', response.data)
}).catch(() => {
commit('SET_AUTHENTICATED', false)
commit('SET_USER', null)
})
},
}
}
The authentication is working but everytime I refresh the page the authentication status is defaulted back to false. How do I ensure that if the user is authenticated the authentication status remains true?