I am trying to perform authentication system using vuex. I was able to implement it but after i login to app and then when i logout, I can still access protected data from server, can help with in correcting my code ? my store.vue file code:
import Vue from 'vue';
import Vuex from 'vuex';
import { getAPI } from '@/axios';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
accessToken: JSON.parse(localStorage.getItem('accessToken')) || null,
APIData: '',
},
mutations: {
// eslint-disable-next-line camelcase
updateStorage(state, access_token) {
// eslint-disable-next-line camelcase
state.accessToken = access_token;
localStorage.setItem('accessToken', JSON.stringify(access_token));
},
destroyToken(state) {
state.accessToken = null;
},
},
getters: {
loggedIn(state) {
return state.accessToken != null;
},
},
actions: {
userLogin(context, usercredentials) {
// eslint-disable-next-line consistent-return
return new Promise((resolve, reject) => {
getAPI.post('/login', {
email: usercredentials.email,
password: usercredentials.password,
})
.then((response) => {
context.commit('updateStorage', { access_token: response.data.access_token });
resolve();
console.log(response.data.access_token);
// console.log(localStorage.getItem('accessToken'));
})
.catch((error) => {
reject(error);
});
});
},
userLogout(context) {
if (context.getters.loggedIn) {
context.commit('destroyToken');
}
},
},
});
last time i got help from @shob