sst / open-next

Open source Next.js serverless adapter
https://open-next.js.org
MIT License
3.7k stars 111 forks source link

(Next)Auth doesn't properly refresh the page when deployed using Open Next #440

Closed paneidos closed 3 weeks ago

paneidos commented 3 weeks ago

When using Auth.js, redirects and responses work differently when deployed on Vercel vs. deploying using SST/OpenNext. This results in login/logout buttons not doing anything on the screen.

Looking in the browsers console, it seems OpenNext returns a redirect response right away, while Vercel splits this in 2 requests. A POST with 200 and then a GET with a 3xx code.

Steps to reproduce

Deploy

  1. Clone https://github.com/paneidos/next-auth-demo
  2. echo "AUTH_SECRET=\"$(openssl rand -base64 32)\"" >> .env
  3. yarn install
  4. yarn sst deploy --stage prod

Use site

  1. Login as test@example.com, password 'hunter2'.
  2. Try to log out immediately (button at the bottom)

Expected

Redirect to login

Actual

Stay on dashboard

conico974 commented 3 weeks ago

We cannot access your reproduction repo. What version of Next, SST and OpenNext are you using ?

paneidos commented 3 weeks ago

My bad I think. I was sure I changed it to public, even entered the 2FA. Anyway, it's public now.

I generated the app with the newest Next.js and followed the instructions on the site.

conico974 commented 3 weeks ago

Couple of things:

paneidos commented 3 weeks ago

That's weird. I also have just the one request on Vercel too. Must've done something wrong, when I tried to extract a smaller sample from the tutorial application.

Can you look at the full app instead? https://github.com/paneidos/nextjs-dashboard The login screen doesn't redirect on my AWS deploy with SST. All the code there is from the official tutorial.

Login with user@nextmail.com, password 123456. Vercel: https://nextjs-dashboard-sandy-xi-58.vercel.app/ AWS: https://d2r9ta6vcll7hb.cloudfront.net/

conico974 commented 3 weeks ago

Your login page is using SSG, it needs to be SSR as well. If this solve it you can close the issue. If you want more advice i'd recommend going into the discord https://sst.dev/discord either in help or in the open-next channel

paneidos commented 3 weeks ago

Thanks for the help, @conico974 Adding export const dynamic = "force-dynamic" to the page.tsx fixed it.

It does seem to have become SSR on the Vercel deploy without extra instructions. Do they auto-detect something to mark it as such? If so, maybe it's possible to bring that detection to open-next?

I understand if you want to close this issue though, as it's got a workaround in being explicit about your intentions. 😄

I'll make certain to remember the Discord while developing a full app.

conico974 commented 3 weeks ago

Vercel put the middleware in front of the CDN inside a cloudflare worker. So you don't have the issue there. Technically it's possible to do the same thing with OpenNext, but this is not documented yet and it requires to write all the IAC by yourself. I'll close this as this is not really an issue