<ProtectedRoute />
The ProtectedRoute component renders its child only when the user is signed in with WSO2 Identity Server. Use it in a TanStack Router route's component to keep signed-out users away from that route.
Overview¶
ProtectedRoute reads the authentication state from AsgardeoProvider and decides what to render in this order:
- While the SDK is still determining the authentication state, it renders
loader(nothing by default). - If the user is signed in, it renders
children. - If the user is signed out and you provided
fallback, it rendersfallback. - If the user is signed out and you provided
redirectTo, it navigates to that path with TanStack Router's<Navigate />. - If you provided neither, it throws an
AsgardeoRuntimeErrorwith the codeProtectedRoute-ValidationError-001.
fallback takes precedence over redirectTo when you pass both. You must pass at least one of them.
Usage¶
ProtectedRoute must be rendered inside both AsgardeoProvider and TanStack Router's RouterProvider.
Redirect signed-out users¶
Send signed-out users to a route of your app, such as a page that renders the SignIn component or a SignInButton.
import { ProtectedRoute } from '@asgardeo/tanstack-router';
import { createRoute } from '@tanstack/react-router';
const dashboardRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/dashboard',
component: () => (
<ProtectedRoute redirectTo="/signin">
<Dashboard />
</ProtectedRoute>
),
});
Show a fallback¶
Render content in place instead of navigating away.
const adminRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/admin',
component: () => (
<ProtectedRoute fallback={<p>Please sign in to view this page.</p>}>
<AdminPanel />
</ProtectedRoute>
),
});
Show a loading state¶
While the SDK initializes, ProtectedRoute renders nothing unless you pass loader.
const dashboardRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/dashboard',
component: () => (
<ProtectedRoute redirectTo="/signin" loader={<Spinner />}>
<Dashboard />
</ProtectedRoute>
),
});
Note
The loader prop is a React element shown while authentication is loading. It isn't related to TanStack Router's route loader function.
Protect a group of routes¶
Use a pathless layout route that wraps an <Outlet /> to protect every child route with one ProtectedRoute.
import { ProtectedRoute } from '@asgardeo/tanstack-router';
import { createRoute, Outlet } from '@tanstack/react-router';
const authenticatedRoute = createRoute({
getParentRoute: () => rootRoute,
id: 'authenticated',
component: () => (
<ProtectedRoute redirectTo="/signin">
<Outlet />
</ProtectedRoute>
),
});
const dashboardRoute = createRoute({
getParentRoute: () => authenticatedRoute,
path: '/dashboard',
component: Dashboard,
});
const routeTree = rootRoute.addChildren([
indexRoute,
signInRoute,
authenticatedRoute.addChildren([dashboardRoute]),
]);
Props¶
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
children |
ReactElement |
✅ | – | Element to render when the user is signed in. |
fallback |
ReactElement |
❌ | – | Element to render when the user is signed out. Takes precedence over redirectTo. |
redirectTo |
string |
❌ | – | Path to navigate to when the user is signed out and no fallback is set. Required if fallback isn't set. |
loader |
ReactNode |
❌ | null |
Content to render while the authentication state is loading. |
Notes¶
- The check runs when the route's component renders. TanStack Router
beforeLoadandloaderfunctions for the route still run for signed-out users, so protect any sensitive data on your API as well. - Unlike the React Router SDK, this
ProtectedRoutedoesn't start sign-in on its own and has nosignInOptionsoronSignInprops. To start sign-in, redirect to a page that uses the React SDK'sSignInButtonorSignIncomponent, or callsignIn()fromuseAsgardeo(). - To protect content inside a page instead of a whole route, use the React SDK's
SignedIncomponent.