fixing auth-routes example

This commit is contained in:
kodekrendel 2017-05-10 23:06:43 -04:00
parent 892543eb1d
commit 2f881dfb31

View File

@ -1,41 +1,45 @@
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios' import axios from 'axios'
export const state = { Vue.use(Vuex)
authUser: null
}
export const mutations = { const store = new Vuex.Store({
SET_USER: function (state, user) { state: {
state.authUser = user authUser: null
} },
} mutations: {
SET_USER: function (state, user) {
export const actions = { state.authUser = user
nuxtServerInit ({ commit }, { req }) {
if (req.session && req.session.authUser) {
commit('SET_USER', req.session.authUser)
} }
}, },
login ({ commit }, { username, password }) { actions: {
return axios.post('/api/login', { nuxtServerInit ({ commit }, { req }) {
username, if (req.session && req.session.authUser) {
password commit('SET_USER', req.session.authUser)
})
.then((res) => {
commit('SET_USER', res.data)
})
.catch((error) => {
if (error.response.status === 401) {
throw new Error('Bad credentials')
} }
}) },
}, login ({ commit }, { username, password }) {
return axios.post('/api/login', {
logout ({ commit }) { username,
return axios.post('/api/logout') password
.then(() => { })
commit('SET_USER', null) .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)
})
}
} }
})
} export default store