Simulating session storage on login
[issue link]A newbie trying hard with nuxtjs here:)
I made an app with nuxtjs with login system via json api (without clientID or cookie) typical PHP browser session. First, login it’s fine, it’s ok, BUT when reload… well, you know, no login.
I have read all the related issues, many similar but I have not just left the mental loop about this. I followed the example of auth routes and my ‘store / index.js’ is exactly the one in the example.
P.D: No problem interacting with $ store.state.authUser in components, pages, etc … until reload is done.
some help?
My store/index.js
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')
}
},
async login ({ commit }, { username, password, submit }) {
try {
const params = new URLSearchParams()
params.append('username', username)
params.append('password', password)
params.append('submit', submit)
const { data } = await axios.post('https://www.xxxxxxxx.com/e/account/login', params, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
commit('SET_USER', data)
} catch (error) {
if (error.response && error.response.status === 401) {
throw new Error('Bad Credentials')
}
throw error
}
}