This tutorial is a generic SaaS path on Stack Auth: install and configure the SDK, wire sign-in, read the current user on the client and server, and gate your product. It does not assume teams, workspaces, or a particular permission model-those live in Build a team-based app and the linked guides below.Documentation Index
Fetch the complete documentation index at: https://stackauth-e0affa27-chore-move-mcp-to-a-sep-app.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
What you will have at the end
- A working sign-in and account flow using Stack Auth’s handler routes (Next.js) or your framework’s equivalent.
- A clear pattern for who is signed in across server components, client components, server actions, and route handlers.
- Protected areas of your app (for example middleware on
/app/*orgetUser({ or: "redirect" })on key routes). - A short map for where multi-tenant and authorization work fits when you need it, plus a mindset for production domains, OAuth, and email.
Prerequisites
- A Next.js project using the App Router (Stack’s first-class path for hosted UI and handlers), or another stack supported in Setup (React, Express, or REST from any backend).
- A Stack Auth account and a project in the dashboard.
Stack does not officially support the Next.js Pages Router. If you are on Pages Router, consider the React or JavaScript SDKs per the FAQ.
StackClientApp / REST calls as in Setup.
1. Install Stack and wire environment variables
The fastest path for JavaScript and TypeScript is the setup wizard:Terminal
.env.local:
.env.local
What the wizard sets up (Next.js)
Afterinit, you should see files similar to:
app/handler/[...stack]/page.tsx- hosted sign-in, sign-up, account settings, and moreapp/layout.tsx- wraps the app withStackProviderandStackThemeapp/loading.tsx- Suspense boundary for Stack’s async hooksstack/server.ts-stackServerAppfor server components, actions, and route handlersstack/client.ts-stackClientAppwhen you need the client app object explicitly
- stack/server.ts
- stack/client.ts
- app/handler/[...stack]/page.tsx
- app/layout.tsx
- app/loading.tsx
stack/server.ts
/handler/sign-up), create a test user, and confirm you land back in your app.
Marketing header: sign in / sign out
UseuseStackApp() so you do not hard-code handler URLs (they can be customized in the project):
components/auth-header.tsx
2. Resolve the signed-in user everywhere
Almost every SaaS screen starts from the current user: profile, preferences, billing state in your database, or admin vs end-user behavior you define yourself.- Server Component
- Server Component (require auth)
- Client Component
- Client Component (require auth)
app/dashboard/page.tsx
Server action that requires a user
{ or: "throw" } is useful when a redirect would be wrong (for example, from a form POST). The example below only needs Stack for identity; your own persistence layer stores product data keyed by user.id.
app/actions/onboarding.ts
Route Handler (App Router API)
app/api/me/route.ts
Middleware for a /app (or /private) section
Match only the routes that should be gated, and exclude /handler so Stack’s auth pages keep working:
middleware.ts
Treat client-side checks as UX only. Anything that mutates data or exposes another customer’s data must be enforced again on the server (server components, server actions, route handlers, or your backend using the secret key or verified tokens).
3. Tenancy, teams, and permissions (when you need them)
Stack gives you users and authentication primitives; your SaaS decides how rows and features map to customers.- Single-user or simple B2C - Often enough to key application data to
user.idand enforce access in your API with the same user you resolved viastackServerApp.getUser(). - Shared accounts, workspaces, or B2B orgs - Use Stack teams as the customer boundary, team selection for the active workspace, and RBAC for roles and fine-grained actions. Walk through that shape in Build a team-based app, with reference material in Teams, Team selection, and RBAC.
4. Product polish: onboarding, email, and optional billing
Hook flows to the guides-no extra Stack APIs are required at this layer:- Onboarding and sign-up rules - User onboarding, Sign-up rules
- Email - Emails
- Stripe / plans - Payments
app/page.tsx or a server layout once getUser() is non-null.
5. Production checklist
Before going live, tighten callback domains, replace shared OAuth keys with your own provider apps where needed, and review email and security defaults. Follow Launch checklist.Related guides
| Topic | Guide |
|---|---|
| Install and configure | Setup |
StackApp object | Stack App |
| Current user and page protection | User fundamentals |
| Teams, membership, and RBAC (deeper path) | Build a team-based app |
| Teams reference | Teams |
| Permissions | RBAC |
| Pre-launch hardening | Launch checklist |
| Billing (optional) | Payments |
| General questions | FAQ |
FAQ
Do I have to use teams for a SaaS?
Do I have to use teams for a SaaS?
No. This guide stays user-centric until you opt into teams. When multiple people share one customer account, follow Build a team-based app and the Teams docs.
Where should I enforce permissions?
Where should I enforce permissions?
Use dashboard-defined permissions for authorization when you use RBAC; always enforce business rules on the server: Server Components, server actions, route handlers, or your backend with the secret server key or validated access tokens. Client checks alone are not enough for sensitive operations.
Can I use Stack only as a backend API?
Can I use Stack only as a backend API?
Yes. Non-JS or custom frontends can use the REST API with the same project keys; the mental model (users, and optionally teams and permissions) stays the same.
How do I test locally with OAuth and redirects?
How do I test locally with OAuth and redirects?
Localhost callback behavior and production domain restrictions are covered under Domains in Launch checklist. Keep localhost allowances enabled only for development.
How does this relate to the team-focused tutorial?
How does this relate to the team-focused tutorial?
Build a team-based app is the place for teams, team selection, and RBAC walkthroughs. This SaaS tutorial covers the generic product path: auth bootstrap, resolving the user, protecting routes, then linking out for tenancy and launch details.
Where do I get help or report doc gaps?
Where do I get help or report doc gaps?
See FAQ for contribution and community pointers.