Skip to content

Commit 40cf986

Browse files
committed
chore: Use shared type helper
1 parent cc8e86d commit 40cf986

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

.changeset/spotty-cooks-march.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,69 @@
22
"@clerk/tanstack-react-start": minor
33
---
44

5-
placeholder trigger test
5+
Added support for [TanStack Start v1 RC](https://tanstack.com/blog/announcing-tanstack-start-v1)! Includes a new `clerkMiddleware()` global middleware replacing the custom server handler.
6+
7+
Usage:
8+
9+
1. Create a `src/start.ts` file and add `clerkMiddleware()` to the list of request middlewares:
10+
11+
```ts
12+
// src/start.ts
13+
import { clerkMiddleware } from '@clerk/tanstack-react-start/server'
14+
import { createStart } from '@tanstack/react-start'
15+
16+
export const startInstance = createStart(() => {
17+
return {
18+
requestMiddleware: [clerkMiddleware()],
19+
}
20+
})
21+
```
22+
23+
2. Add `<ClerkProvider>` to your root route
24+
25+
```tsx
26+
// src/routes/__root.tsx
27+
import { ClerkProvider } from '@clerk/tanstack-react-start'
28+
29+
export const Route = createRootRoute({...})
30+
31+
function RootDocument({ children }: { children: React.ReactNode }) {
32+
return (
33+
<ClerkProvider>
34+
<html>
35+
<head>
36+
<HeadContent />
37+
</head>
38+
<body>
39+
{children}
40+
<Scripts />
41+
</body>
42+
</html>
43+
</ClerkProvider>
44+
)
45+
}
46+
```
47+
48+
The `getAuth()` helper can now be called within server routes and functions, without passing a Request object:
49+
50+
```ts
51+
const authStateFn = createServerFn().handler(async () => {
52+
const { userId } = await getAuth()
53+
54+
if (!userId) {
55+
throw redirect({
56+
to: '/sign-in',
57+
})
58+
}
59+
60+
return { userId }
61+
})
62+
63+
export const Route = createFileRoute('/')({
64+
component: Home,
65+
beforeLoad: async () => await authStateFn(),
66+
loader: async ({ context }) => {
67+
return { userId: context.userId }
68+
},
69+
})
70+
```

integration/templates/tanstack-react-start/src/routes/user.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { createFileRoute, redirect } from '@tanstack/react-router';
22
import { createServerFn } from '@tanstack/react-start';
33
import { getAuth } from '@clerk/tanstack-react-start/server';
4-
import { getRequest } from '@tanstack/react-start/server';
54

65
const fetchClerkAuth = createServerFn({ method: 'GET' }).handler(async () => {
7-
const { userId } = await getAuth(getRequest());
6+
const { userId } = await getAuth();
87

98
return {
109
userId,

packages/tanstack-react-start/src/server/getAuth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import type { SessionAuthObject } from '@clerk/backend';
12
import type { AuthOptions, GetAuthFnNoRequest } from '@clerk/backend/internal';
23
import { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';
34
import { getGlobalStartContext } from '@tanstack/react-start';
45

5-
import { clerkMiddlewareNotConfigured, noFetchFnCtxPassedInGetAuth } from '../utils/errors';
6+
import { errorThrower } from '../utils';
7+
import { clerkMiddlewareNotConfigured } from '../utils/errors';
68

7-
export const getAuth: GetAuthFnNoRequest<{}, true> = (async (opts?: AuthOptions) => {
9+
export const getAuth: GetAuthFnNoRequest<SessionAuthObject, true> = (async (opts?: AuthOptions) => {
810
// @ts-expect-error: Untyped internal Clerk start context
911
const authObjectFn = getGlobalStartContext().auth;
1012

@@ -16,4 +18,4 @@ export const getAuth: GetAuthFnNoRequest<{}, true> = (async (opts?: AuthOptions)
1618
const authObject = await Promise.resolve(authObjectFn({ treatPendingAsSignedOut: opts?.treatPendingAsSignedOut }));
1719

1820
return getAuthObjectForAcceptedToken({ authObject, acceptsToken: opts?.acceptsToken });
19-
}) as GetAuthFn<Request, true>;
21+
}) as GetAuthFnNoRequest<SessionAuthObject, true>;

0 commit comments

Comments
 (0)