auth-module icon indicating copy to clipboard operation
auth-module copied to clipboard

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

Open RobinBertilsson opened this issue 6 years ago • 26 comments

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

RobinBertilsson avatar Oct 14 '20 19:10 RobinBertilsson

I have the same problem too

albertarni avatar Oct 15 '20 08:10 albertarni

@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 avatar Oct 15 '20 09:10 albertarni

@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..

RobinBertilsson avatar Oct 15 '20 09:10 RobinBertilsson

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

RobinBertilsson avatar Oct 15 '20 11:10 RobinBertilsson

Downgrading didn't do anything for me

fabiom91 avatar Oct 20 '20 13:10 fabiom91

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

fabiom91 avatar Oct 20 '20 14:10 fabiom91

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 avatar Oct 22 '20 17:10 israelroldan

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

RobinBertilsson avatar Oct 22 '20 17:10 RobinBertilsson

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

just-tom avatar Oct 25 '20 14:10 just-tom

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.

velidonmez avatar Oct 25 '20 16:10 velidonmez

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
    }
  },
  .
  .
}

velidonmez avatar Oct 26 '20 08:10 velidonmez

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, }, } },

just-tom avatar Oct 26 '20 11:10 just-tom

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

just-tom avatar Nov 03 '20 08:11 just-tom

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...

simonmaass avatar Nov 04 '20 11:11 simonmaass

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

g0rk4 avatar Nov 05 '20 05:11 g0rk4

Duplicate of https://github.com/nuxt-community/auth-module/issues/809

msonowal avatar Dec 14 '20 08:12 msonowal

Latest version fixes this close this issue

msonowal avatar Dec 15 '20 06:12 msonowal

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.

JoaoPedroAS51 avatar Dec 16 '20 16:12 JoaoPedroAS51

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

Eve-rine avatar Jan 10 '21 22:01 Eve-rine

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

azmatzuberi avatar Apr 18 '21 18:04 azmatzuberi