Nuxt/examples/auth-routes/store/index.js
Sébastien Chopin 17650c25e0 Middleware feature 🔥
2017-02-03 15:09:38 +01:00

42 lines
779 B
JavaScript

import axios from 'axios'
export const state = {
authUser: null
}
export const mutations = {
SET_USER: function (state, user) {
state.authUser = user
}
}
export const actions = {
nuxtServerInit ({ commit }, { req }) {
if (req.session && req.session.authUser) {
commit('SET_USER', req.session.authUser)
}
},
login ({ commit }, { username, password }) {
return axios.post('/api/login', {
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)
})
}
}