Supabase Auth with Next.js
I recently set up my app using this guide:
https://supabase.com/docs/reference/auth-helpers/next-js
I'm redirecting the user to a
/login
page if they end up on a protected route using withMiddlewareAuth
For some reason, and I've only noticed this today and am trying to figure out what I did, even after the user logs in, you still get redirected to /login
, unless you do a refresh of the page. Is this expected behavior? What could be causing it? thanks!Supabase Auth with Next.js | Supabase
This submodule provides convenience helpers for implementing user authentication in Next.js applications.
14 Replies
Hi :vmathi: The team informed me some minutes ago an issue with the auth part. It is probably this. It should be fixed asap @jamielarkspur1995
oh lovely I was going insane for a second haha. Is is tracked somewhere? Curious what it was
It is an internal stuff, so it is tracked internally π
But should be fixed asap
kk no worries! π . As an aside, I've been using
router.back()
to redirect post login. Do you know a nicer way to go back to exactly to what the user was trying to do? Including query params and such? Or should I parse that manually and do a router.push()
. Thanks so much! πYou should do router.push. As I recall, auth-helpers set a redirectFrom parameter when you get redirected from a protected page.
Just parse that and push on success login
I forgot to answer, but i was thinking
router.back
was enough. NanoBit is probably right tho, i would use his wayThanks for the answers guys! I still need to refresh, so I'm assuming it's a hairy bug. The openness is really appreciated π
Not a Next.js issue, but I've been having absolute hell with Vue/Vue Router because there's no async method for checking the user session in supabase-js β this looks like it might be solved in the supabase-js 2 RC which I'm going to try but I've had to hack like crazy β don't know if this is related?
If the issue is that you need a refresh before you actually are authenticated and are using auth-helpers,
I recommend setting a delay 100-200ms before router.push to allow cookies to be set first
I recommend you create a separate thread. Perhaps itβll allow it to be seen by more people .
Sorry to open an old thread again, is this a bug or expected behavior, anyone know
I handle this with an
authenticated
route that handles my redirect. https://github.com/jensen/supabase-nextjsGitHub
GitHub - jensen/supabase-nextjs
Contribute to jensen/supabase-nextjs development by creating an account on GitHub.
@jensen Thanks for providing your project, how did you implement the sign out? Everytime we use the supabaseClient from the useSessionContext but it called /api/logout like v1
Signout can be found here https://github.com/jensen/supabase-nextjs/blob/main/components/dashboard/logout.tsx
I didn't notice anything wrong with it yet. I just threw this together recently though.
These are my package depdencies.
@jensen Thank you so much!