sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.31k stars 164 forks source link

fix: authState getting wiped on page reload on static websites #785

Open Vijayabhaskar96 opened 4 months ago

Vijayabhaskar96 commented 4 months ago

Previous PR: #712

πŸ”— Linked issue

551

This issue occurs because data (data from getSession()) is not stored in the browser, which is not a problem in SSR with server because the server takes care of it, but on a statically generated website, this data is lost along with rawToken even if the token is stored in the browser cookies if you refresh the webpage.

❓ Type of change

πŸ“š Description

This PR stores the retrieved data from getSession() and stores it in the browser cookie auth:sessionCookie and reloads the data and rawToken back on the client side if they're undefined. This PR fixes the data issue, but you still need to set the prerender:false for middleware protected routes in routeRules so that client-side middleware is forced to run and load the states.

πŸ“ Checklist

Geekimo commented 3 months ago

Hello @zoey-kaiser, is there anything we can do to help get this fix shipped ?

Thanks !

zoey-kaiser commented 3 months ago

Hi @Geekimo πŸ‘‹

I would like to get a review by @phoenix-ru in before merging this PR 😊

Geekimo commented 3 months ago

@zoey-kaiser Thanks for your reply ☺️

thorge commented 2 months ago

Any news on this fix or can we help? I use a local provider and having auth work with ssg would be highly appreciated :-)

Geekimo commented 2 months ago

Hello @zoey-kaiser and @phoenix-ru, Sorry to bother you about this fix, but I'm currently holding a deployment to production due to this bug. I'd really love to help you out to get this released.

Kind regards.

pkg-pr-new[bot] commented 1 month ago

Open in Stackblitz

pnpm add https://pkg.pr.new/@sidebase/nuxt-auth@785

commit: 0f7e51a

zoey-kaiser commented 1 month ago

Hi @Vijayabhaskar96 πŸ‘‹

I wanted to push this PR into the next release, but sadly the authjs tests are failing. We will finalize the PRs for the release tomorrow. For the case that we are not able to include it yet, you can directly install your PR as an npm package using:

pnpm add https://pkg.pr.new/@sidebase/nuxt-auth@785
Geekimo commented 1 month ago

Hello @zoey-kaiser, will this PR be included in the next release (if yes, when is it planned ?) Regards

thorge commented 3 weeks ago

Is there a specific reason why this wasn't included in the 0.9.4 release?