module: ^5.0.0-1611574754.9020f2a
nuxt: ^2.14.12
I'm using auth-next with Laravel Sanctum and Laravel Fortify.
Login by itself works, but I have issues with login after registration / reset-password.
The following is the expected behaviour. In v.4.9.1 this used to work. Now in v5.x the fetchUser method does not perform the user request and therefore doesn't log in in nuxt-auth, however I am logged in server-side / Laravel.
this.$axios.get('/sanctum/csrf-cookie').then(() => {
this.$axios
.post('/register', this.form)
.then(() => {
// Registration successful
this.$auth.fetchUser().then(() => {
this.$router.push('/dashboard')
})
})
.catch((errors) => {
// Registration failed
console.log(errors)
})
})
This also doesn't work:
this.$axios.get('/sanctum/csrf-cookie').then(() => {
this.$axios
.post('/register', this.form)
.then(() => {
// Registration successful
this.$axios
.get('/api/user')
.then((response) => {
this.$auth.setUser(response.data.data)
this.$router.push('/dashboard')
})
.catch((errors) => {
console.log(errors)
})
})
.catch((errors) => {
// Registration failed
console.log(errors)
})
})
The following is only a workaround, but not ideal:
this.$axios.get('/sanctum/csrf-cookie').then(() => {
this.$axios
.post('/register', this.form)
.then(() => {
// Registration successful
this.$auth.logout().then(() => {
this.$auth
.loginWith('laravelSanctum', {
data: this.form,
})
.then(() => {
// Login successful
})
.catch((errors) => {
// Login failed
console.log(errors)
})
})
})
.catch((errors) => {
// Registration failed
console.log(errors)
})
})
Any update on this problem?
await this.$auth.setUserToken(true)
set this after registration complete
Hi guys! I think we just need to set the token flag before fetching the user. Can you try the following code?
this.$axios.get('/sanctum/csrf-cookie').then(() => {
this.$axios
.post('/register', this.form)
.then(() => {
// Set token flag and fetch user
this.$auth.setUserToken(true)).then(() => {
this.$router.push('/dashboard')
})
})
.catch((errors) => {
// Registration failed
console.log(errors)
})
})
Let me know if it worked! :)
EDIT: the setUserToken also fetches the user
Most helpful comment
Hi guys! I think we just need to set the token flag before fetching the user. Can you try the following code?
Let me know if it worked! :)
EDIT: the
setUserTokenalso fetches the user