I have a problem with the authenticate with my Nuxt application (static SSR).
I'm using Sanctum. But the $auth variable is emptied after a refresh. So the user is disconnected.
login.vue
await axios.get(`${process.env.apiUrl}/sanctum/csrf-cookie`)
const res = await this.$auth.loginWith('laravelSanctum', {
data: {
email: this.userLogin.login.email,
password: this.userLogin.login.password,
}
})
.catch((error) => {
this.userLogin.messageError = 'Wrong credentials'
[ ... stop process code ...]
})
await this.$auth.$storage.setUniversal('_auth.user', JSON.stringify(res.data.user))
await this.$auth.setUser(res.data.user)
store/index.js
export const actions = {
async nuxtServerInit({ commit, dispatch }) {
const user = this.$auth.$storage.getUniversal('_auth.user')
if (user) {
await this.$auth.setUser(user)
}
}
}
nuxt.config.js
auth: {
strategies: {
laravelSanctum: {
provider: 'laravel/sanctum',
url: process.env.apiUrl,
endpoints: {
csrf: { url: '/sanctum/csrf-cookie', methods: 'GET' },
login: { url: '/api/login', method: 'POST' },
logout: { url: '/api/logout', method: 'POST' },
user: false
},
user: {
property: false,
autoFetch: false
},
cookie: false
}
},
redirect: {
login: '/mon-compte/login',
logout: '/mon-compte/login',
//home: '/mon-compte/mon-espace',
register: '/mon-compte/register'
}
}