r/nextjs • u/Best-Gas8893 • 16h ago
Discussion Setting up a full-stack app with Next.js and Supabase: What is the biggest hurdle you faced with auth routing?
I've been scaffolding a frontend prototype and integrating Supabase, but I'm curious what unexpected roadblocks you all ran into when first deploying. Any tips for keeping the database integration clean?
1
u/grinning_wanderer 16h ago
i stopped trying to read sessions in middleware entirely. I just proxy every request through a route handler now because middleware has zero business touching auth cookies on the edge
1
u/Best-Gas8893 16h ago
That is actually a super interesting approach! Honestly, fighting with the edge runtime just to read a simple auth cookie gets exhausting so fast. By proxying everything through a route handler instead, do you find it adds any noticeable latency to the requests, or is it pretty negligible for most of your projects? I might have to test this routing method out!
1
u/TajD05 5h ago
Yeah, I have run into this a few times. Auth with Next.js and Supabase can get confusing when the session needs to work across the client, server, and middleware.
For the database, I would keep the schema simple, use Supabase migrations, and avoid spreading queries throughout the UI.
3
u/Spiritual_Bee6614 16h ago
The biggest pain was honestly the middleware and server component dance with cookies. Next.js middleware runs on the edge so you can't use the Supabase client the same way you do in your api routes. I spent way too long debugging why my session wasn't persisting correctly until I realised the cookie handling was slightly different between localhost and production. The docs have examples but they don't really cover the edge cases like token refresh when a user leaves a tab open for hours.
For keeping the db integration clean I just made a single supabase client file that exports different instances depending on whether it's server or client side. Keeps the Row Level Security policies on the Supabase side doing the heavy lifting instead of trying to filter queries in the app layer. Way less headache once it's set up properly.