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
Olyno
Olynoβ€’3y ago
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
jamielarkspur1995
jamielarkspur1995OPβ€’3y ago
oh lovely I was going insane for a second haha. Is is tracked somewhere? Curious what it was
Olyno
Olynoβ€’3y ago
It is an internal stuff, so it is tracked internally πŸ˜… But should be fixed asap
jamielarkspur1995
jamielarkspur1995OPβ€’3y ago
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! πŸ™‚
NanoBit
NanoBitβ€’3y ago
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
Olyno
Olynoβ€’3y ago
I forgot to answer, but i was thinking router.back was enough. NanoBit is probably right tho, i would use his way
jamielarkspur1995
jamielarkspur1995OPβ€’3y ago
Thanks for the answers guys! I still need to refresh, so I'm assuming it's a hairy bug. The openness is really appreciated πŸ™‚
GaryLake
GaryLakeβ€’3y ago
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?
NanoBit
NanoBitβ€’3y ago
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 .
jamielarkspur1995
jamielarkspur1995OPβ€’3y ago
Sorry to open an old thread again, is this a bug or expected behavior, anyone know
jensen
jensenβ€’3y ago
I handle this with an authenticated route that handles my redirect. https://github.com/jensen/supabase-nextjs
GitHub
GitHub - jensen/supabase-nextjs
Contribute to jensen/supabase-nextjs development by creating an account on GitHub.
COP
COPβ€’3y ago
@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
jensen
jensenβ€’3y ago
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.
"@supabase/auth-helpers-nextjs": "^0.4.1",
"@supabase/auth-helpers-react": "^0.3.0",
"@supabase/supabase-js": "^2.0.0",
"@supabase/auth-helpers-nextjs": "^0.4.1",
"@supabase/auth-helpers-react": "^0.3.0",
"@supabase/supabase-js": "^2.0.0",
COP
COPβ€’3y ago
@jensen Thank you so much!

Did you find this page helpful?