Auth-module: [@nuxtjs/auth-next] - Auth middleware issue with SSR

Created on 14 Oct 2020  路  20Comments  路  Source: nuxt-community/auth-module

I'm using @nuxtjs/auth-next in my Nuxt application.
The application is configured using universal mode (or ssr: true since mode: 'universal' is obsolete)

I have a super simple page called pages/test.vue, which looks like

<template>
  <div>Hello World!</div>
</template>

<script lang="ts">
import { defineComponent } from 'nuxt-composition-api'

export default defineComponent({
  middleware: 'auth',
})
</script>

I've also tested without Composition API, which looks like this:

import Vue from 'vue'

export default Vue.extend({
  middleware: 'auth',
})

It all works fine without SSR enabled, but when I enable it the auth middleware doesn't work and redirects me to the login page instead.

I can verify that I'm authenticated since I can access the user via $auth.user, and I also have a valid non-expired token in LocalStorage.

After digging a bit deeper into the auth-next/dist/core/middleware.js it seems like ctx.$auth.$state.loggedIn is false.
Can someone please help me?

Version
5.0.0-1595976864.509d9d6

Most helpful comment

It seems this is not only limited to auth-next, I was just helping debug this same issue in an application that uses v4.x

All 20 comments

I have the same problem too

@RobinBertilsson
OK, I found the source of this issue.
If you downgrade your @axios module to 5.12.1, then it should work.

https://github.com/nuxt-community/auth-module/issues/809#issuecomment-687750325

@albertarni, thanks.

Sadly I still facing the same issue.
The middleware keeps redirecting me to /login...

Thinking if it could be related to a release of the auth-next package as well?
I'll try to downgrade a few versions and see, I'll keep you updated..

Sadly no luck with an older version of auth-next...

Downgrading didn't do anything for me

I have found a solution without downgrading axios: https://github.com/nuxt-community/auth-module/issues/478#issuecomment-712912500

It seems this is not only limited to auth-next, I was just helping debug this same issue in an application that uses v4.x

@israelroldan really? Did the Axios downgrade solution work for you?

Having the same issue, happens only on page hard refresh.

Seems the auth middleware on page refresh gets called before the auth module has chance to verify authentication, hence the redirect

I think this is not related to auth-next. I created a project from scratch to test this. Only installed nuxt-auth and nuxt-axios. Still have the issue. Also suggested solutions above didn't work for me.

Ok, for my case setting expire time for auth options solved the problem. I added cookie options to nuxt.config file. The default value for cookies was Session and that was causing the problem.

auth: {
  cookie: {
    options: {
      expires: 10
    }
  },
  .
  .
}

It's definitely a hydration timing issue between SSR and client side. I can clearly see the logged in state being hydrated after the page has loaded, at which point the middleware has already run for auth, and because client side hydration happening after this point is sees as not logged in and redirects to login.

Im using auth-next but pretty sure I had the same issue with 4.* although I cannot remember how I solved it annoyingly. Right now im using the sanctum strategy very simply.

auth: { strategies: { 'laravelSanctum': { provider: 'laravel/sanctum', url: process.env.API_URL, }, } },

@albertarni downgrade solution on the Axios module worked for me.

we just encountered the exact same problem - same setup as @just-tom

we also just downgraded to axios nuxt module to 5.12.1... and now SSR works as expected...

the change that might cause this looks like it is related to upgrading axios from 0.19.2 to 0.20.0...

I also encountered the exact problem and the downgrade of axios nuxt module to 5.12.1 worked for me too

Latest version fixes this close this issue

Closing here since latest version of auth-next should solve this issue. @nuxtjs/axios updated to v5.12.4. Feel free to reopen if issue persists.

I have an issue with @nuxt/auth-next my login details are being retrieved successfuly bt am not being logged in bt when i use @nuxt/auth everything works fine,what could be the problem please

I鈥檓 still having trouble with this. The route is protected and $auth.loggedIn === true but it keeps redirecting me to the homepage. I鈥檝e tried updating all my packages but so far no luck.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pi0 picture pi0  路  3Comments

dasisyouyu picture dasisyouyu  路  3Comments

aaronhuisinga picture aaronhuisinga  路  3Comments

javialon26 picture javialon26  路  3Comments

weijinnx picture weijinnx  路  3Comments