|
I'm working on the authentication system for Advuman. Currently, we have the login and signup pages in place, but I'm unsure about the best approach for:
What would be the recommended approach for implementing this in a Next.js application with Supabase backend? |
Replies: 2 comments
|
Recommended approach (Next.js + Supabase Auth)
Let Supabase Auth issue and rotate JWTs. Use HTTP-only cookies via @supabase/auth-helpers-nextjs instead of localStorage. Access tokens are short-lived; refresh happens automatically.
Use Supabase’s server-side session helpers in: middleware.ts (protect routes) Server Components / Route Handlers (validate user) Treat Supabase as the single source of truth for sessions. Avoid custom session tables unless you need advanced analytics.
Enable Google provider in Supabase Auth. Use PKCE flow (default in Supabase) for security. Handle redirects through Supabase; Next.js only processes the callback. Always map OAuth users to internal profiles via a profiles table.
Never trust client JWTs blindly—verify on the server using Supabase helpers. Protect data with Postgres Row Level Security (RLS) (this is critical). API routes should: Read the session Enforce role/ownership checks Avoid exposing service role keys to the client. Best practices Use middleware-based route protection instead of client checks. Keep secrets in server-only env vars. Prefer RLS over application-level authorization. Add rate limiting on auth-related routes. Log auth events (sign-in, sign-out, failures). Suggested stack Supabase Auth (JWT + OAuth) @supabase/auth-helpers-nextjs RLS-enabled Postgres tables Next.js Middleware for protected routes Bottom line: |
|
Advuman already has a solid authentication foundation: the Express backend provides register, login, and current-user endpoints, hashes passwords with bcrypt, validates JWTs through middleware, and applies role-based authorization for Client, Analyst, and Admin users. For production, I would implement the flow as follows: The user submits the login or registration form from the React frontend. The current seven-day JWT approach is acceptable for the Lite development version, but production should add short-lived access tokens, refresh-token rotation, login rate limiting, email verification, password reset, restricted CORS configuration, and a mandatory strong JWT_SECRET. The frontend should also centralize authentication in an AuthProvider rather than letting individual pages manage tokens independently. The existing API already exposes /register, /login, and /me |
Recommended approach (Next.js + Supabase Auth)
Let Supabase Auth issue and rotate JWTs.
Use HTTP-only cookies via @supabase/auth-helpers-nextjs instead of localStorage.
Access tokens are short-lived; refresh happens automatically.
Use Supabase’s server-side session helpers in:
middleware.ts (protect routes)
Server Components / Route Handlers (validate user)
Treat Supabase as the single source of truth for sessions.
Avoid custom session tables unless you need advanced analytics.
Enable Google provider in Supabase Auth.
Use PKCE flow (default in Supabase) for security.
Handle redirects through Supabase; Next.js only …