TanStack Router with Clerk #1119
Replies: 3 comments 3 replies
-
Have you figured out the solution? |
Beta Was this translation helpful? Give feedback.
-
https://tanstack.com/router/v1/docs/framework/react/examples/start-clerk-basic |
Beta Was this translation helpful? Give feedback.
-
I found two possible solutions: 1. Use This approach checks the token during the route loading phase and redirects the user if the token is missing. import { createFileRoute, Outlet, redirect } from '@tanstack/react-router';
export const Route = createFileRoute('/_authed')({
beforeLoad: async ({ context }) => {
// Fetch the token before rendering the route
const token = await context.auth.getToken();
// Redirect to sign-in if token is missing
if (!token) {
throw redirect({
to: '/sign-in',
});
}
},
component: Outlet,
}); 2. Use Clerk's This approach is more declarative, using Clerk's built-in components to conditionally render based on the user's signed-in state. import { SignedIn, SignedOut } from '@clerk/clerk-react';
import { createFileRoute, Navigate, Outlet } from '@tanstack/react-router';
export const Route = createFileRoute('/_authed')({
component: AuthedLayout,
});
function AuthedLayout() {
return (
<>
{/* Render Outlet if user is signed in */}
<SignedIn>
<Outlet />
</SignedIn>
{/* Redirect to sign-in if user is signed out */}
<SignedOut>
<Navigate to="/sign-in" />
</SignedOut>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
I am trying to test out TanStack Router with Clerk and facing some difficulties with the auth and using the the clerk or convex hooks. Has anyone had any luck at setting up auth? I find that I get stuck in an infinite loop of continuously logging in. I have seen a similar post regarding clerk on here and tried that implementation, however no luck.
This is using the latest packages as of today (brand new app via Vite).
This is my
__root.tsx
This is the
index.tsx
This is the
App.tsx
And finally this is the
router.tsx
Beta Was this translation helpful? Give feedback.
All reactions