Skip to main content

Protecting Routes

You're reading Identity & Access Management 7.2.0. The newest release is 7.3.0, and Cloud has the newest features. Go to the newest release

This guide shows how to protect routes in a TanStack Router app with the WSO2 Identity Server TanStack Router SDK, using either code-based or file-based routing. It assumes you have already set up AsgardeoProvider by following the WSO2 Identity Server React quickstart.

Install the SDK​

npm install @asgardeo/tanstack-router @asgardeo/react @tanstack/react-router

Provide authentication to the router​

AsgardeoProvider must wrap RouterProvider so that ProtectedRoute can read the authentication state.

main.tsx
import { AsgardeoProvider } from '@asgardeo/react';
import { createRouter, RouterProvider } from '@tanstack/react-router';
import { createRoot } from 'react-dom/client';
import { routeTree } from './routeTree.gen';

const router = createRouter({ routeTree });

createRoot(document.getElementById('root')!).render(
<AsgardeoProvider
baseUrl="https://api.asgardeo.io/t/<your-org>"
clientId="<your-client-id>"
>
<RouterProvider router={router} />
</AsgardeoProvider>,
);

With code-based routing, build routeTree with rootRoute.addChildren([...]) instead of importing the generated file.

Protect a group of routes​

Instead of wrapping every protected page, create a pathless layout route whose component is a ProtectedRoute around an <Outlet />. Every child route then requires a signed-in user.

src/routes/_authenticated.tsx
import { ProtectedRoute } from '@asgardeo/tanstack-router';
import { createFileRoute, Outlet } from '@tanstack/react-router';

export const Route = createFileRoute('/_authenticated')({
component: () => (
<ProtectedRoute redirectTo="/signin" loader={<p>Loading...</p>}>
<Outlet />
</ProtectedRoute>
),
});

Put protected pages under src/routes/_authenticated/, for example src/routes/_authenticated/dashboard.tsx for /dashboard.

Add the sign-in page​

ProtectedRoute in this package doesn't start sign-in itself, so give redirectTo a page that does. For example, show a SignInButton from the React SDK and send signed-in users onward:

SignInPage.tsx
import { SignedIn, SignedOut, SignInButton } from '@asgardeo/react';
import { Navigate } from '@tanstack/react-router';

export default function SignInPage() {
return (
<>
<SignedOut>
<p>Sign in to continue.</p>
<SignInButton />
</SignedOut>
<SignedIn>
<Navigate to="/dashboard" />
</SignedIn>
</>
);
}
tip

If you don't need a sign-in page, use fallback to show content in place of the protected route instead. See ProtectedRoute for all options.

Handle embedded sign-in redirects​

If your sign-in page uses the React SDK's embedded SignIn component and the flow redirects to an external page, add a CallbackRoute at the path the flow returns to:

src/routes/callback.tsx
import { CallbackRoute } from '@asgardeo/tanstack-router';
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/callback')({
component: CallbackRoute,
});

What protection covers​

ProtectedRoute decides what to render on the client. A protected route's beforeLoad and loader functions still run for signed-out users, and the browser can still request your API directly. Always validate access tokens on your server or API as well. See Accessing protected APIs.

Was this page helpful?