возможно Вам это поможет
vuex
const auth = {
state: {
authUser: null
},
actions: {
nuxtServerInit ({ commit}, {req}) {
if (req.session && req.session.authUser) {
commit('SET_USER', req.session.authUser)
}
},
login ({ commit, username, password}) {
return axios.post('/api/loin', {
username,
password
})
.then((res) => {
commit('SET_USER', res.data)
})
.catch((error) => {
if (error.response.status === 401) {
throw new Error('Bad credentials')
}
})
},
logout({ commit }) {
return axios.post('/api/logout')
.then(() => {
commit('SET_USER', null)
})
}
},
mutations: {
SET_USER: function (state, user) {
state.authUser = user
}
}
};
vue
methods: {
login () {
this.$store.dispatch('login', {
username: this.formUsername,
password: this.formPassword,
})
.then(() => {
this.formUsername = '';
this.formPassword = '';
this.formError = null;
this.$router.push({ path: '/summary' });
})
.catch((e) => {
this.formError = e.message
});
},
logout () {
this.$store.dispatch('logout')
}
}