Open History to see the last commit
1import { createFileRoute, redirect } from '@tanstack/solid-router'
2import { Show } from 'solid-js'
3β
4export const Route = createFileRoute('/protected')({
5 beforeLoad: ({ context }) => {
6 if (!context.session) {
7 throw redirect({ to: '/login' })
8 }
9 },
10 component: Protected,
11})
12β
13function Protected() {
14 const routeContext = Route.useRouteContext()
15β
16 return (
17 <div class="max-w-2xl mx-auto">
18 <h1 class="text-3xl font-bold mb-4">Protected Page</h1>
19 <p class="text-gray-600 mb-8">
20 This page is only accessible to authenticated users.
21 </p>
22β
23 <div class="bg-green-50 border border-green-200 rounded-lg p-6">
24 <h2 class="text-xl font-semibold mb-4 text-green-800">
25 Welcome, {routeContext().session?.user?.name ?? 'User'}!
26 </h2>
27β
28 <Show when={routeContext().session?.user}>
29 {(user) => (
30 <div class="space-y-2 text-green-700">
31 <p>
32 <strong>Email:</strong> {user().email ?? 'N/A'}
33 </p>
34 <Show when={user().image}>
35 {(image) => (
36 <div>
37 <strong>Avatar:</strong>
38 <img
39 src={image()}
40 alt="User avatar"
41 class="w-20 h-20 rounded-full mt-2"
42 />
43 </div>
44 )}
45 </Show>
46 </div>
47 )}
48 </Show>
49 </div>
50β
51 <div class="mt-8 p-4 bg-gray-100 rounded-lg">
52 <h3 class="font-semibold mb-2">Session Data (Debug)</h3>
53 <pre class="text-xs overflow-auto bg-gray-800 text-green-400 p-4 rounded">
54 {JSON.stringify(routeContext().session, null, 2)}
55 </pre>
56 </div>
57 </div>
58 )
59}