What you will have at the end
- A clear model for protecting pages, layouts, route handlers, and server actions—and when to use middleware vs in-render checks.
- Awareness of Next.js + sensitive HTML so you do not leak data through composition.
- A secrets and environment split that keeps the server key out of the browser.
- A launch-aligned checklist (domains, OAuth, email, production mode) with pointers to deeper docs.
- A webhook verification mindset (signed payloads only).
1. Protect a page (and know what that actually guarantees)
UseuseUser({ or: "redirect" }) in a Client Component. This lets Hexclave capture the current
browser URL and establish the PKCE handoff before navigating to hosted authentication.
app/app/dashboard/page.tsx
redirect vs throw
{ or: "redirect" }— use withuseUser. ThegetUseroverload is deprecated on both client and server app objects because app type describes permissions, not whether the call runs in a browser.{ or: "throw" }— use in server actions, route handlers, and other places where navigation is not the server code’s responsibility; map errors to401/403responses yourself.
app/api/me/route.ts
Sensitive content and client-side rendering
Client Components ship to the browser. Gating one withuseUser controls navigation, but does not
hide its bundled code or authorize protected backend operations. Keep secrets out of client code and
enforce access in the server action, route handler, or backend serving the data.
Read the full discussion in User fundamentals — Protecting a page.
Treat client-side checks as UX only. Anything that mutates data or exposes another customer’s data must be enforced in server actions, route handlers, or your backend with the secret server key or validated tokens. For team-scoped apps, re-check RBAC on the server, not only with
usePermission.2. Secrets, keys, and environments
Practical split:
Use separate Stack projects or at least separate env values for production vs staging when possible. Rotate keys from the dashboard if a secret is exposed.
3. Domains, OAuth, email, and production mode
Stack’s dev defaults (localhost callbacks, shared OAuth keys, shared mail) are convenient but not what you want for real users.1
Domains and callbacks
Add your real
https://… origin under Domain & Handlers and disable Allow all localhost callbacks when you no longer need local redirects against production configuration. Details: Launch checklist — Domains.2
OAuth providers
Create your own OAuth clients per provider, set the provider callback URLs Stack documents, then paste your client ID and secret in the dashboard (leave shared keys for local dev only). Details: Launch checklist — OAuth providers and Auth providers.
3
Point outbound mail at your SMTP/domain so magic links and invitations come from a domain users trust. Details: Launch checklist — Email server and Emails.
4
Enable production mode
After the above, turn on production mode in Project Settings so dashboard guardrails match how you run in prod (Launch checklist — Enabling production mode).
4. Webhooks
If you consume Stack webhooks, verify every payload (for example with Svix andSTACK_WEBHOOK_SECRET) before acting on events—treat unsigned or failed verification as 400. Implementation patterns: Webhooks.
5. Before you flip traffic
- Smoke-test sign-in, sign-up, password reset, and OAuth on the production domain after DNS and env vars are final.
- Confirm redirect URLs in third-party consoles (OAuth, IdP) match the Stack callback URLs you use in prod.
- Align session / cookie behavior with your hosting (same-site, HTTPS, reverse proxies) per your platform docs.
- Plan rollback: keep prior env values or a maintenance window note so you can revert dashboard or env changes quickly.
Related guides
FAQ
Is middleware enough to protect my API?
Is middleware enough to protect my API?
Middleware runs on matching routes and is great for coarse “must be logged in” gates. Route handlers and server actions should still call
getUser({ or: "throw" }) (or equivalent) and return proper HTTP errors—middleware will not run for every internal call path, and authorization (what that user may do) belongs next to your business logic.Where do teams and RBAC fit for launch?
Where do teams and RBAC fit for launch?
Model permissions in the dashboard, then enforce them on the server for every sensitive operation. Walkthrough: Build a team-based app.