Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClerkProvider, Show, UserButton, SignInButton } from '@clerk/react-router';
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router'
import { isRouteErrorResponse, Link, Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router'
import { clerkMiddleware, rootAuthLoader } from '@clerk/react-router/server'

import type { Route } from './+types/root'
Expand Down Expand Up @@ -53,6 +53,7 @@ export default function App({ loaderData }: Route.ComponentProps) {
</Show>
{/* Show the user button when the user is signed in */}
<Show when="signed-in">
<Link to="/protected">Protected</Link>
<UserButton />
</Show>
</header>
Expand Down
7 changes: 5 additions & 2 deletions app/routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { type RouteConfig, index } from "@react-router/dev/routes";
import { type RouteConfig, index, route } from "@react-router/dev/routes";

export default [index("routes/home.tsx")] satisfies RouteConfig;
export default [
index("routes/home.tsx"),
route("protected", "routes/protected.tsx"),
] satisfies RouteConfig;
26 changes: 26 additions & 0 deletions app/routes/protected.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { redirect } from 'react-router'
import { getAuth } from '@clerk/react-router/server'
import type { Route } from './+types/protected'

export async function loader(args: Route.LoaderArgs) {
// Use `getAuth()` to read the session on the server and protect this route.
// Unlike the `<Show>` component, which only controls what renders, this is
// the real access check: a signed-out user who navigates here is redirected.
const { isAuthenticated, userId } = await getAuth(args)

if (!isAuthenticated) {
return redirect('/')
}

return { userId }
}

export default function Protected({ loaderData }: Route.ComponentProps) {
return (
<main className="flex items-center justify-center px-4 py-8">
<p>
Welcome! Your user ID is <code>{loaderData.userId}</code>.
</p>
</main>
)
}