Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After logging in I am redirected to login page when refreshing the page #754

Open
yuelongh opened this issue May 15, 2024 · 6 comments
Open
Labels
bug A bug that needs to be resolved pending

Comments

@yuelongh
Copy link

yuelongh commented May 15, 2024

Environment


  • Operating System: Linux
  • Node Version: v21.7.3
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: yarn@1.22.19
  • Builder: -
  • User Config: vite, typescript, devtools, app, pwa, modules, formkit, wcmcUserManagement, i18n, tailwindcss, build, runtimeConfig, experimental, alias, dirs, css
  • Runtime Modules: @vite-pwa/nuxt@0.6.0, @vueuse/nuxt@10.9.0, @nuxt/test-utils/module@3.12.0, @nuxtjs/stylelint-module@5.2.0, @nuxtjs/eslint-module@4.1.0, @pinia/nuxt@0.5.1, @unepwcmc/user-management@0.0.39, @nuxtjs/tailwindcss@6.11.4, @nuxtjs/i18n@8.3.0, @formkit/nuxt@1.6.2
  • Build Modules: -

Reproduction

Refresh Page after logging in

Describe the bug

Settings for nuxt-auth

{
      isEnabled: true,
      baseURL: process.env.AUTH_ORIGIN,
      provider: {
        type: 'authjs'
      },
      globalAppMiddleware: {
        isEnabled: true,
        allow404WithoutAuth: true
      }
    }

I am experiencing a weird behaviour that after logging in if I don't refresh the page everything works fine.
However, once I refresh the page then I can see from browser dev tool the value of __Host-next-auth.csrf-token keeps changing then it redirects me to the login page and becomes unauthenticated but it does now keep changing when running in dev mode

I found a solution from another post it did help a bit when so when I have following on my page then it remaining authenticated after refreshing the page but still redirects me to login page

const { getSession } = useAuth()
onMounted(() => {
  getSession({ force: true })
})

Additional context

nuxt-auth versions used 6.3, 7.0, 7.1, 7.2

I have the globalAppMiddleware set to true and I noticed that when const { status } = useAuth(); is called in middleware
node_modules/@sidebase/nuxt-auth/dist/runtime/middleware/auth.mjs status returns unauthenticated then it redirects me to the sign in page but I am already signed in at the point.

Reason of the behaviour -- updated on 21May

After some investigations that I found out because I have Nginx reverse proxy.
So Nginx appends the header of "x-forwarded-proto": "HTTP"
if I force that to be "x-forwarded-proto": "HTTPS" then everything works fine again so it was "x-forwarded-proto" causing the issue I was wondering if anyone knows the reason behind this?

Logs

No response

@yuelongh yuelongh added bug A bug that needs to be resolved pending labels May 15, 2024
@yuelongh yuelongh changed the title Cannot set headers after they are sent to the client on production server After logging in I keep redirected to login page May 16, 2024
@yuelongh yuelongh changed the title After logging in I keep redirected to login page After logging in I am redirected to login page when refreshing the page May 16, 2024
@mbellamyy
Copy link

I have the same issue with refresh. Super super frustrating.

@yuelongh
Copy link
Author

yuelongh commented May 21, 2024

I have the same issue with refresh. Super super frustrating.

have you tried to add x-forwarded-proto: https from your proxy manager?

to help debug

I also tried to copy the codes in the middleware
/node_modules/@sidebase/nuxt-auth/dist/runtime/middleware/auth.mjs

and use the code in the file to recreate my own global middleware in my Nuxt to see all values and to see where was the problem that's where I found status becomes unauthenticated in the middleware when x-forwarded-proto header has HTTP as the value

make sure you have this structure 01.xxxxxx.global.ts as the file name so it can be triggered before anything else + globally

@mbellamyy
Copy link

mbellamyy commented May 21, 2024

I have the same issue with refresh. Super super frustrating.

have you tried to add x-forwarded-proto: https from your proxy manager?

to help debug

I also tried to copy the codes in the middleware /node_modules/@sidebase/nuxt-auth/dist/runtime/middleware/auth.mjs

and use the code in the file to recreate my own global middleware in my Nuxt to see all values and to see where was the problem that's where I found status becomes unauthenticated in the middleware when x-forwarded-proto header has HTTP as the value

make sure you have this structure 01.xxxxxx.global.ts as the file name so it can be triggered before anything else + globally

I have not enabled the globalMiddleware from sidebase-auth settings.

I also don't have a reverse proxy, although I do have a server middleware (in nuxt) that redirects http to https.
It exactly checks if x-forwarded-proto is HTTP and redirects to https if it is. But I can't seem to be able to trigger that case even if I explicitly use http in the request.

I'm dealing with this in production. Whenever I deploy my app, everything is fine, people log in and stay logged in across refreshs. But after a couple of hours, it's your post happening all over again.

@dev-davexoyinbo

This comment was marked as off-topic.

@mbellamyy
Copy link

This is quite an important issue on quite a basic requirement.
#744 and #743 and #732 seem to be the same problem.
Can someone please look into this?

@dev-davexoyinbo

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved pending
Projects
None yet
Development

No branches or pull requests

3 participants