mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-02 02:17:15 +00:00
fixing auth-routes example
This commit is contained in:
parent
892543eb1d
commit
2f881dfb31
@ -1,41 +1,45 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import axios from 'axios'
|
||||
|
||||
export const state = {
|
||||
authUser: null
|
||||
}
|
||||
Vue.use(Vuex)
|
||||
|
||||
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)
|
||||
const store = new Vuex.Store({
|
||||
state: {
|
||||
authUser: null
|
||||
},
|
||||
mutations: {
|
||||
SET_USER: function (state, user) {
|
||||
state.authUser = user
|
||||
}
|
||||
},
|
||||
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')
|
||||
actions: {
|
||||
nuxtServerInit ({ commit }, { req }) {
|
||||
if (req.session && req.session.authUser) {
|
||||
commit('SET_USER', req.session.authUser)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
logout ({ commit }) {
|
||||
return axios.post('/api/logout')
|
||||
.then(() => {
|
||||
commit('SET_USER', null)
|
||||
})
|
||||
},
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
export default store
|
||||
|
Loading…
Reference in New Issue
Block a user