mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 10:04:05 +00:00
e7cc2757c3
Co-authored-by: Alexander Lichter <manniL@gmx.net>
38 lines
850 B
JavaScript
38 lines
850 B
JavaScript
import axios from 'axios'
|
|
|
|
export const state = () => ({
|
|
authUser: null
|
|
})
|
|
|
|
export const mutations = {
|
|
SET_USER (state, user) {
|
|
state.authUser = user
|
|
}
|
|
}
|
|
|
|
export const actions = {
|
|
// nuxtServerInit is called by Nuxt.js before server-rendering every page
|
|
nuxtServerInit ({ commit }, { req }) {
|
|
if (req.session && req.session.authUser) {
|
|
commit('SET_USER', req.session.authUser)
|
|
}
|
|
},
|
|
async login ({ commit }, { username, password }) {
|
|
try {
|
|
const { data } = await axios.post('/api/login', { username, password })
|
|
commit('SET_USER', data)
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 401) {
|
|
throw new Error('Bad credentials')
|
|
}
|
|
throw error
|
|
}
|
|
},
|
|
|
|
async logout ({ commit }) {
|
|
await axios.post('/api/logout')
|
|
commit('SET_USER', null)
|
|
}
|
|
|
|
}
|