You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8cf5541
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/router/guide/authenticated-routes.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: Authenticated Routes
5
5
6
6
Authentication is an extremely common requirement for web applications. In this guide, we'll walk through how to use TanStack Router to build protected routes, and how to redirect users to login if they try to access them.
7
7
8
-
> **A route guard does not protect a server function.** This guide shows how to gate a route's UI behind `beforeLoad`. If you also have a `createServerFn` that fetches sensitive data, the route guard does NOT stop a direct request to that RPC — server functions are reachable by POST regardless of which route renders them. Pair routing-side guards with **handler-level enforcement** (auth middleware or in-handler check). See [Authentication Server Primitives](../../start/framework/react/guide/authentication-server-primitives.md) in the Start docs.
8
+
> **A route guard is not a data authorization boundary.** This guide shows how to gate a route's UI behind `beforeLoad`. Any server function, server route, or API endpoint that returns private data must authorize the request itself, because it can be requested independently of the route that calls it. See [Authentication Server Primitives](../../start/framework/react/guide/authentication-server-primitives.md) in the Start docs.
Copy file name to clipboardExpand all lines: docs/start/framework/react/guide/authentication-overview.md
+15-12Lines changed: 15 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,11 +72,11 @@ title: Authentication
72
72
- Good for mixed public/private content on same route
73
73
- Requires careful handling to prevent layout shifts
74
74
75
-
**Server Function Guards**
75
+
**Data/API Protection (Security Boundary)**
76
76
77
-
-Server-side validation before executing sensitive operations
78
-
-Works alongside route-level protection
79
-
-Essential for API security regardless of client-side protection
77
+
-Authorize every server function, server route, or API endpoint that reads or writes private data
78
+
-Reject unauthorized requests even if no protected route was loaded first
79
+
-Treat route guards as UX and navigation control, not as the data boundary
80
80
81
81
### State Management Patterns
82
82
@@ -207,14 +207,17 @@ Build your own authentication system using TanStack Start's server functions and
207
207
- Specific business logic needs
208
208
- Full ownership of authentication data
209
209
210
-
### Security Considerations
211
-
212
-
- Use HTTPS in production
213
-
- Use HTTP-only cookies when possible
214
-
- Validate all inputs on the server
215
-
- Keep secrets in server-only functions
216
-
- Implement rate limiting for auth endpoints
217
-
- Use CSRF protection for form submissions
210
+
### Production Auth Checklist
211
+
212
+
- Use HTTPS in production and set a strong session secret.
213
+
- Store sessions in `HttpOnly`, `Secure`, `SameSite` cookies. Do not store session tokens in `localStorage` or `sessionStorage`.
214
+
- Enforce auth in every server function, server route, or API endpoint that reads or writes private user, tenant, or account data. Use `beforeLoad` for page UX, not as the data boundary.
215
+
- Use `.inputValidator()` on every server function that accepts input.
216
+
- Hash passwords with bcrypt, scrypt, or Argon2. For missing users, verify against a dummy hash and return the same login/reset message.
217
+
- Rate limit login, registration, and password-reset endpoints.
218
+
- Use CSRF or same-origin protections for non-GET server functions and server routes.
219
+
- Log authentication events and monitor failures.
220
+
- Test direct unauthenticated calls to protected server functions; they should reject before returning data.
Copy file name to clipboardExpand all lines: docs/start/framework/react/guide/authentication-server-primitives.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,14 @@ This guide covers the **server-side primitives** for building authentication in
7
7
8
8
If you can use a managed solution like [Clerk](https://go.clerk.com/wOwHtuJ) or [WorkOS](https://workos.com/), prefer that — they handle most of what this guide describes. Read on if you're rolling your own.
9
9
10
-
## The Two Halves of Auth
10
+
## Protect Data First
11
11
12
-
Authentication has a routing half and a server half. They both need to be implemented; either one alone is incomplete.
12
+
Authentication has a data/API boundary and a route/UI layer. The data boundary is the security boundary: every server function, server route, or API endpoint that reads or writes private data must authorize the request before returning data or mutating state.
13
13
14
-
-**Routing half** (`router-core/auth-and-guards`): redirect unauthenticated users away from protected pages, gate UI on roles/permissions, surface a login form.
15
-
-**Server half** (this guide): issue and verify session cookies, exchange OAuth codes, hash and verify passwords, rate-limit credential endpoints, defeat user enumeration.
14
+
-**Data/API boundary** (this guide): issue and verify session cookies, exchange OAuth codes, hash and verify passwords, rate-limit credential endpoints, defeat user enumeration, and authorize private data access.
15
+
-**Route/UI layer** (`router-core/auth-and-guards`): redirect unauthenticated users away from screens they cannot use, gate UI on roles/permissions, surface a login form, and avoid triggering requests that would fail anyway.
16
16
17
-
> **A route guard does NOT protect a `createServerFn` on that route.** Server functions are RPC endpoints — they're reachable via direct POST regardless of which route renders them. Auth must be enforced on the handler (or via middleware), not on the calling route.
17
+
> **A route guard is not a data authorization boundary.** Server functions and server routes are API endpoints; they are reachable independently of the route that calls them. Auth must be enforced in the handler or middleware for the endpoint that touches private data. `beforeLoad` is for route UX.
Copy file name to clipboardExpand all lines: docs/start/framework/react/guide/authentication.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,8 @@ Authentication involves many considerations including password security, session
36
36
37
37
TanStack Start provides the tools for both through server functions, sessions, and route protection.
38
38
39
+
> Protect the data/API boundary first. Any server function, server route, or other API endpoint that returns or mutates private data must authorize the request itself. `beforeLoad` is useful for route UX: it keeps users out of screens they cannot use and avoids triggering work that would fail anyway. It is not the security boundary for the data. See [Authentication Server Primitives](./authentication-server-primitives.md) for the server-side pattern.
Copy file name to clipboardExpand all lines: docs/start/framework/react/guide/middleware.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -822,7 +822,7 @@ Static middlewares are created once and reused across routes. A middleware facto
822
822
823
823
This middleware validates the session and injects it into `context` for downstream middlewares.
824
824
825
-
> **Attach `authMiddleware` to every `createServerFn` that needs auth.**A route `beforeLoad` redirect protects the page experience but does NOT protect the RPC — server functions are reachable via direct POST regardless of which route renders them. Pair routing-side guards with handler-level enforcement here. See [Authentication Server Primitives](./authentication-server-primitives.md).
825
+
> **Attach `authMiddleware` to every `createServerFn` that needs auth.**Server functions are API endpoints, so protect the endpoint that reads or mutates private data. A route `beforeLoad` guard improves route UX, but it is not the data boundary. See [Authentication Server Primitives](./authentication-server-primitives.md).
Copy file name to clipboardExpand all lines: docs/start/framework/react/guide/server-functions.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -397,7 +397,7 @@ Use server functions without JavaScript by leveraging the `.url` property with H
397
397
398
398
Compose server functions with middleware for authentication, logging, and shared logic. See the [Middleware guide](./middleware.md).
399
399
400
-
> **Auth must be enforced on the server function, not the route.**A `createServerFn` is an RPC endpoint reachable by direct POST regardless of which route renders the calling UI — a route `beforeLoad` redirect protects the page experience, but it does not stop a request from hitting the RPC directly. Apply `authMiddleware` (or an equivalent in-handler check) to every server function that needs auth. See [Authentication Server Primitives](./authentication-server-primitives.md).
400
+
> **Protect data in the endpoint that serves it.**Server functions are API endpoints reachable independently of whichever route renders the calling UI. Apply `authMiddleware` or an equivalent in-handler check to every server function that reads or writes private data. `beforeLoad` is useful route UX, but it is not the data boundary. See [Authentication Server Primitives](./authentication-server-primitives.md).
Copy file name to clipboardExpand all lines: docs/start/framework/solid/guide/authentication.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,8 @@ Authentication involves many considerations including password security, session
36
36
37
37
TanStack Start provides the tools for both through server functions, sessions, and route protection.
38
38
39
+
> Protect the data/API boundary first. Any server function, server route, or other API endpoint that returns or mutates private data must authorize the request itself. `beforeLoad` is useful for route UX: it keeps users out of screens they cannot use and avoids triggering work that would fail anyway. It is not the security boundary for the data. See [Authentication Server Primitives](./authentication-server-primitives.md) for the server-side pattern.
Copy file name to clipboardExpand all lines: packages/router-core/skills/router-core/auth-and-guards/SKILL.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,9 @@ sources:
21
21
22
22
# Auth and Guards
23
23
24
-
> **This skill covers the routing side of auth.** For the **server-side primitives** — session cookies (`HttpOnly`/`Secure`/`SameSite`), `useSession`-style helpers, OAuth `state` + PKCE, password-reset enumeration defense, CSRF, rate limiting — see [start-core/auth-server-primitives](../../../../start-client-core/skills/start-core/auth-server-primitives/SKILL.md). The two skills are designed to be used together.
24
+
> **This skill covers the routing side of auth.**Route guards are UX and navigation control; the data/API boundary still belongs in the server function, server route, or API endpoint that reads or mutates private data. For the **server-side primitives** — session cookies (`HttpOnly`/`Secure`/`SameSite`), `useSession`-style helpers, OAuth `state` + PKCE, password-reset enumeration defense, CSRF, rate limiting — see [start-core/auth-server-primitives](../../../../start-client-core/skills/start-core/auth-server-primitives/SKILL.md).
25
25
>
26
-
> **CRITICAL**: A route guard (`beforeLoad`) does NOT protect a `createServerFn` declared on that route. Server functions are RPC endpoints reachable by direct POST regardless of which route renders them. See "Route guards do not protect server functions" below.
26
+
> **CRITICAL**: A route guard (`beforeLoad`) does NOT protect a `createServerFn` declared on that route. Server functions are API endpoints reachable independently of the route that calls them. See "Route guards do not protect server functions" below.
Rule of thumb: every `createServerFn`that touches user data needs `authMiddleware` (or an equivalent in-handler check). The route guard is for the page experience; the RPC guard is for the data. See [start-core/auth-server-primitives](../../../../start-client-core/skills/start-core/auth-server-primitives/SKILL.md) for the full session/middleware pattern.
411
+
Rule of thumb: every `createServerFn`, server route, or API endpoint that touches user data needs `authMiddleware` (or an equivalent in-handler check). The route guard is for the page experience; the endpoint guard is for the data. See [start-core/auth-server-primitives](../../../../start-client-core/skills/start-core/auth-server-primitives/SKILL.md) for the full session/middleware pattern.
412
412
413
413
### HIGH: Auth check in component instead of beforeLoad
0 commit comments