manchenkoff / nuxt-auth-sanctum

Nuxt module for Laravel Sanctum authentication
https://manchenkoff.gitbook.io/nuxt-auth-sanctum/
MIT License
175 stars 22 forks source link

[Bug] Authenticated user redirected to login & home when refreshing the page #37

Closed briansmith7 closed 8 months ago

briansmith7 commented 9 months ago

I'm experiencing a strange issue when using v0.1.2 of the package:

When an authenticated user refreshes the sanctum:auth protected page or opens it in a new tab - they are immediately redirected to login page, and a few seconds later redirected to onGuestOnly value (i.e. home page) and at this point I see that useSanctumUser is loaded and user is authenticated.

Strangely, I'm only experiencing this in production, but not in local development environment. Reproducing the same steps locally opens the sanctum:auth protected page immediately, without redirecting to login & home.

It seems that middleware rules are evaluated before useSanctumUser variable is initialized, which happens only after a series of redirects.

I would appreciate any suggestions on how to debug what's causing this.

manchenkoff commented 9 months ago

Hey @briansmith7, are you using SSR or CSR mode in production? and which Nuxt version is there?

briansmith7 commented 9 months ago

@manchenkoff it looks like you are right, this is caused by SSR.

I added the following to my nuxt.config.ts and this seems to have resolved the issue:

routeRules: {
        '/app/**': {  ssr: false },
}

Many thanks for you quick response!

manchenkoff commented 9 months ago

No problem @briansmith7, but this is a bug if it works like that. The module is supposed to work in both modes, so I will reopen it to double-check the correctness of the behavior.

briansmith7 commented 9 months ago

@manchenkoff Sure! Let me know if you need any inputs on my config to help you reproduce this

manchenkoff commented 9 months ago

@manchenkoff Sure! Let me know if you need any inputs on my config to help you reproduce this

Nuxt version would help 👍 It might be related to the order of initializing or speed of loading, I'll check on the same version with yours.

briansmith7 commented 9 months ago

I'm using 3.8.0

SDIjeremy commented 9 months ago

I'm having the same issue, was actually going to open an issue for this today.

SDIjeremy commented 9 months ago

@manchenkoff it looks like you are right, this is caused by SSR.

I added the following to my nuxt.config.ts and this seems to have resolved the issue:

routeRules: {
        '/app/**': {  ssr: false },
}

Many thanks for you quick response!

So is the solution to have the /app/ in it's own directory without SSR? and the rest of the auth pages in another directory with SSR?

briansmith7 commented 9 months ago

In my case all of the sanctum:auth protected pages are under /app/**, login and signup are in the root directory, other pages without middleware (blog, etc) are in their own directories

manchenkoff commented 9 months ago

I will check that behavior, meanwhile you can check if v0.0.11 works better, if so, I would be almost sure about the problem.

briansmith7 commented 9 months ago

I tried v0.0.11 but experienced the same bug when SSR is enabled. Disabling SSR for sanctum:auth routes also fixed the issue

SDIjeremy commented 9 months ago

I'm using latest version v0.1.2 now and am not having this issue anymore.

manchenkoff commented 8 months ago

I'm closing this issue since it is always related to the backend configuration when CSR / SSR works differently, please double-check everything in the checklist here, and try checking out these issues: #34, #57.

P.S.

Feel free to reopen this issue with more details (now I have a predefined Bug template), but usually the difference in domains you are using while sending a request from Nuxt CSR / SSR to Laravel.