502 Errors with updated createServerClient code #30185
Replies: 6 comments 17 replies
-
What kind of sign in/sign up are you doing when this issue happens? is it when doing email/password, OAuth, magic link...? Are there any errors in your NextJS terminal? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply No errors in NextJS terminal. This happens post sign in. The user signs in (user and password) and can navigate site. Sometime later (annoyingly intermittent) users get a 502 when navigating between pages. At that point either I have to 1) re-login 2) delete cookie and re-login or 3) comment out the code (as above) |
Beta Was this translation helpful? Give feedback.
-
It looks like the issue might be related to the updateSession part in the official documentation. Could I take a look at the file that’s causing the problem? I’ve uploaded a code to my public GitHub repository that uses Supabase auth with Next.js. Feel free to refer to it, as it might be helpful. I’ve gone through a lot of the official documentation related to auth, so I’m happy to assist if you need further help! |
Beta Was this translation helpful? Give feedback.
-
so I changed the code back to supabase recommended plus added auth: {persistSession: true,} plus granular logging and error handling (see below). initial and all successful middleware calls give this log
After an hour or so I get 502 with this log
utilities/supabase/middleware.ts
|
Beta Was this translation helpful? Give feedback.
-
Given up ......... unless anyone has any bright ideas. We have traced the problem to middleware not updating cookies to refresh token on expiry of JWT token. Supabase logs show a refresh token being issued, but no matter how much log and error trapping we put into the getAll() and SetAll() functions the same (original JWT) token is returned by code in middleware. middleware with error trapping and logging
Produces these console log entries
After days of research and testing the only workaround we can come up with is to extend the JWT expiry time and add an automatic logout for any session that exceeds the expiry time. Seems a shame as the suggested supabase middleware code seems to work in production, but we can't take the risk that users get repeated 502 pages. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Description
When using the recommended
createServerClient
pattern in Next.js supabase/middleware/updateSession.Setting cookies on both the request and response (or response alone) causes intermittent 502 errors.
Environment
Current Recommended Code
Issue
This code results in intermittent 502 errors, which can be cured by deleting cookies on the site and re-logging on. Once a 502 error is triggered the only working solution (apart from deleting cookies) we've found is to set cookies only on the request:
Concerns
While the working solution prevents 502 errors, we're concerned about:
Questions
Beta Was this translation helpful? Give feedback.
All reactions