mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 18:13:54 +00:00
70 lines
1.3 KiB
JavaScript
70 lines
1.3 KiB
JavaScript
import Vue from 'vue'
|
|
import Vuex from 'vuex'
|
|
|
|
Vue.use(Vuex)
|
|
|
|
// Polyfill for window.fetch()
|
|
require('whatwg-fetch')
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
state: {
|
|
authUser: null
|
|
},
|
|
|
|
mutations: {
|
|
SET_USER: function (state, user) {
|
|
state.authUser = user
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
|
|
nuxtServerInit ({ commit }, { req }) {
|
|
if (req.session && req.session.authUser) {
|
|
commit('SET_USER', req.session.authUser)
|
|
}
|
|
},
|
|
|
|
login ({ commit }, { username, password }) {
|
|
return fetch('/api/login', {
|
|
// Send the client cookies to the server
|
|
credentials: 'same-origin',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
username,
|
|
password
|
|
})
|
|
})
|
|
.then((res) => {
|
|
if (res.status === 401) {
|
|
throw new Error('Bad credentials')
|
|
} else {
|
|
return res.json()
|
|
}
|
|
})
|
|
.then((authUser) => {
|
|
commit('SET_USER', authUser)
|
|
})
|
|
},
|
|
|
|
logout ({ commit }) {
|
|
return fetch('/api/logout', {
|
|
// Send the client cookies to the server
|
|
credentials: 'same-origin',
|
|
method: 'POST'
|
|
})
|
|
.then(() => {
|
|
commit('SET_USER', null)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export default store
|