router-core-auth-and-guards

Implement route protection, redirects, and RBAC guards in TanStack Router applications.

Updated Dec 21, 2025
One-click install
npx skills add https://github.com/Angael/veles --skill router-core-auth-and-guards-angael
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: router-core-auth-and-guards
Source: https://github.com/Angael/veles/tree/main/.agents/skills/router-core-auth-and-guards
Command: npx skills add https://github.com/Angael/veles --skill router-core-auth-and-guards-angael

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-router, @tanstack/react-start.

What problem does it solve? Unprotected routes in TanStack Router apps expose private pages and cause flashes of protected content when auth checks run inside components. This Skill provides the correct patterns for guarding routes with beforeLoad, redirecting unauthenticated users, and enforcing role- and permission-based access control. ## Core Features & Use Cases - Redirect-Based Route Guards: Protect routes with beforeLoad and redirect() inside a pathless _authenticated layout route, with redirect-back support via validated search params. - RBAC and Permission Checks: Extend router context with hasRole, hasAnyRole, and hasPermission helpers to build admin-only and permission-gated layout routes. - Auth State via Router Context: Inject live auth state through createRootRouteWithContext and RouterProvider's context prop without recreating the router. - Use Case: You are building a dashboard where only admins can manage users. Create an _authenticated/_admin layout route whose beforeLoad checks context.auth.hasRole('admin') and redirects others to /unauthorized, while every createServerFn handler enforces auth via middleware. ## Quick Start Protect my TanStack Router dashboard routes so unauthenticated users are redirected to a login page and only admins can access the user management section.

Frequently Asked Questions about router-core-auth-and-guards

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I protect routes in TanStack Router?

Create a pathless _authenticated layout route with a beforeLoad function that throws redirect({ to: '/login' }) when context.auth.isAuthenticated is false. Any route file placed under src/routes/_authenticated/ is automatically protected.

How do I implement role-based access control in TanStack Router?

Extend your auth state with hasRole, hasAnyRole, and hasPermission helpers, then check them in beforeLoad of nested layout routes like _authenticated/_admin. Redirect unauthorized users to an /unauthorized page with the original location saved in search params.

Does a beforeLoad route guard protect createServerFn server functions?

No. A beforeLoad guard only protects the route's UI; createServerFn produces an RPC endpoint reachable by direct POST regardless of the route. Every server function touching user data needs authMiddleware or an equivalent in-handler auth check.

Why does my redirect get swallowed in beforeLoad try/catch?

redirect() works by throwing, so a surrounding try/catch captures it. Import isRedirect from @tanstack/react-router and re-throw the error when isRedirect(error) returns true, handling only genuine errors in the catch block.

How do I redirect back to the original page after login?

Save location.href in the redirect's search params, then validate it on the login route with validateSearch. Sanitize the target to reject values not starting with a single forward slash to prevent open redirect attacks.

Why does protected content flash before the login redirect?

Auth checks inside components run after rendering begins, causing a flash of protected content. Move the check to beforeLoad, which runs before any component rendering and before the loader, preventing the flash entirely.