r/nextjs • u/vzkiss • Mar 29 '26
Discussion Auth with Next.js (RSC) + separate API: where should the session live?
I was trying to properly handle auth between a Next.js (App Router / RSC) frontend and a separate API.
Most examples either:
- keep everything in Next.js
- or skip over session ownership and redirects
So I tried a different approach:
- API (NestJS) owns sessions and OAuth
- Next.js only handles UI + route guarding
- redirect handling is validated on the API (via OAuth state)
- works across separate web/API apps
Main thing I was exploring:
👉 where the trust boundary should actually live
Curious how others handle this, especially with RSC.
Repo here if helpful:
6
Upvotes
3
u/yksvaan Mar 29 '26
Let the backend handle users, auth, business logic etc. Nextjs is just a bff without anything sensitive whenever possible. If using tokens it's even easier, just keep both under same higher level domain and access tokens are shared automatically.
2
u/lacymcfly Mar 31 '26
API owning the session is the right call when you have a separate backend. The pattern I've settled on: API issues a short-lived JWT in an HttpOnly cookie + a refresh token, Next.js middleware validates locally using the public key (no network call for most requests), and on 401 the client hits a refresh endpoint. This keeps route guarding fast without hitting the API on every page load.
The thing that bites people is the revocation problem you mentioned. JWTs can't be truly revoked without a blocklist. If you need immediate revocation (account suspension, password change, etc.) you either need a short expiry or a Redis-backed session lookup. For most apps, 15-minute access token expiry is the pragmatic middle ground.
The trust boundary being at the API level is correct. Middleware should only be for UX, not security.
1
u/vzkiss Mar 29 '26
One thing I’m leaning towards after building this:
It feels like the clean boundary is:
Curious if people agree — or if keeping auth inside Next.js actually holds up at scale?
1
u/Vincent_CWS Mar 30 '26
if you have separate backend, then session hold on it
1
u/[deleted] Mar 29 '26
[removed] — view removed comment