Open History to see the last commit
1import { Box, Button, Card, Container, Flex, Theme } from '@radix-ui/themes';
2import { HeadContent, Link, Outlet, Scripts, createRootRoute } from '@tanstack/react-router';
3import { TanStackRouterDevtools } from '@tanstack/react-router-devtools';
4import { Suspense } from 'react';
5import { getSignInUrl } from '@workos/authkit-tanstack-react-start';
6import { AuthKitProvider, getAuthAction } from '@workos/authkit-tanstack-react-start/client';
7import Footer from '../components/footer';
8import SignInButton from '../components/sign-in-button';
9import appCssUrl from '../app.css?url';
10import type { ReactNode } from 'react';
11β
12export const Route = createRootRoute({
13 head: () => ({
14 meta: [
15 {
16 charSet: 'utf-8',
17 },
18 {
19 name: 'viewport',
20 content: 'width=device-width, initial-scale=1',
21 },
22 {
23 title: 'AuthKit Example in TanStack Start',
24 },
25 ],
26 links: [{ rel: 'stylesheet', href: appCssUrl }],
27 }),
28 loader: async () => {
29 // getAuthAction() returns auth state without accessToken, safe for client
30 // Pass to AuthKitProvider as initialAuth to avoid loading flicker
31 const auth = await getAuthAction();
32 const url = await getSignInUrl();
33 return {
34 auth,
35 url,
36 };
37 },
38 component: RootComponent,
39 notFoundComponent: () => <div>Not Found</div>,
40});
41β
42function RootComponent() {
43 const { auth, url } = Route.useLoaderData();
44 return (
45 <RootDocument>
46 <AuthKitProvider initialAuth={auth}>
47 <Theme accentColor="iris" panelBackground="solid" style={{ backgroundColor: 'var(--gray-1)' }}>
48 <Container style={{ backgroundColor: 'var(--gray-1)' }}>
49 <Flex direction="column" gap="5" p="5" height="100vh">
50 <Box asChild flexGrow="1">
51 <Card size="4">
52 <Flex direction="column" height="100%">
53 <Flex asChild justify="between">
54 <header>
55 <Flex gap="4">
56 <Button asChild variant="soft">
57 <Link to="/">Home</Link>
58 </Button>
59β
60 <Button asChild variant="soft">
61 <Link to="/account">Account</Link>
62 </Button>
63β
64 <Button asChild variant="soft">
65 <Link to="/client">Client Demo</Link>
66 </Button>
67 </Flex>
68β
69 <Suspense fallback={<div>Loading...</div>}>
70 <SignInButton user={auth.user} url={url} />
71 </Suspense>
72 </header>
73 </Flex>
74β
75 <Flex flexGrow="1" align="center" justify="center">
76 <main>
77 <Outlet />
78 </main>
79 </Flex>
80 </Flex>
81 </Card>
82 </Box>
83 <Footer />
84 </Flex>
85 </Container>
86 </Theme>
87 <TanStackRouterDevtools position="bottom-right" />
88 </AuthKitProvider>
89 </RootDocument>
90 );
91}
92β
93function RootDocument({ children }: Readonly<{ children: ReactNode }>) {
94 return (
95 <html lang="en">
96 <head>
97 <HeadContent />
98 </head>
99 <body>
100 {children}
101 <Scripts />
102 </body>
103 </html>
104 );
105}