Skip to content

Commit 4ac31b9

Browse files
fix(cli): improve better-auth and polar implementation in tanstack start (#629)
1 parent f0c9101 commit 4ac31b9

File tree

4 files changed

+100
-52
lines changed

4 files changed

+100
-52
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { authMiddleware } from "@/middleware/auth";
2+
import { createServerFn } from "@tanstack/react-start";
3+
4+
export const getUser = createServerFn({ method: "GET" }).middleware([authMiddleware]).handler(async ({ context }) => {
5+
return context.session
6+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { auth } from "@{{projectName}}/auth";
2+
import { createMiddleware } from "@tanstack/react-start";
3+
4+
5+
export const authMiddleware = createMiddleware().server(async ({ next, request }) => {
6+
const session = await auth.api.getSession({
7+
headers: request.headers,
8+
})
9+
return next({
10+
context: { session }
11+
})
12+
})
Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,84 @@
1+
import { getUser } from "@/functions/get-user";
2+
{{#if (eq payments "polar") }}
13
import { Button } from "@/components/ui/button";
24
import { authClient } from "@/lib/auth-client";
3-
{{#if (eq api "trpc")}}
5+
import { getPayment } from "@/functions/get-payment";
6+
{{/if}}
7+
{{#if (eq api "trpc") }}
48
import { useTRPC } from "@/utils/trpc";
59
import { useQuery } from "@tanstack/react-query";
610
{{/if}}
7-
{{#if (eq api "orpc")}}
11+
{{#if (eq api "orpc") }}
812
import { orpc } from "@/utils/orpc";
913
import { useQuery } from "@tanstack/react-query";
1014
{{/if}}
1115
import { createFileRoute, redirect } from "@tanstack/react-router";
1216

1317
export const Route = createFileRoute("/dashboard")({
14-
component: RouteComponent,
15-
beforeLoad: async () => {
16-
const session = await authClient.getSession();
17-
if (!session.data) {
18-
redirect({
19-
to: "/login",
20-
throw: true
21-
});
22-
}
23-
{{#if (eq payments "polar")}}
24-
const {data: customerState} = await authClient.customer.state()
25-
return { session, customerState };
26-
{{else}}
27-
return { session };
28-
{{/if}}
29-
}
18+
component: RouteComponent,
19+
beforeLoad: async () => {
20+
const session = await getUser();
21+
{{#if (eq payments "polar") }}
22+
const customerState = await getPayment();
23+
return { session, customerState };
24+
{{else}}
25+
return { session };
26+
{{/if}}
27+
},
28+
loader: async ({ context }) => {
29+
if (!context.session) {
30+
throw redirect({
31+
to: "/login",
32+
});
33+
}
34+
},
3035
});
3136

3237
function RouteComponent() {
33-
const { session{{#if (eq payments "polar")}}, customerState{{/if}} } = Route.useRouteContext();
38+
const { session{{#if (eq payments "polar") }}, customerState{{/if}} } = Route.useRouteContext();
3439

35-
{{#if (eq api "trpc")}}
36-
const trpc = useTRPC();
37-
const privateData = useQuery(trpc.privateData.queryOptions());
38-
{{/if}}
39-
{{#if (eq api "orpc")}}
40-
const privateData = useQuery(orpc.privateData.queryOptions());
41-
{{/if}}
40+
{{#if (eq api "trpc") }}
41+
const trpc = useTRPC();
42+
const privateData = useQuery(trpc.privateData.queryOptions());
43+
{{/if}}
44+
{{#if (eq api "orpc") }}
45+
const privateData = useQuery(orpc.privateData.queryOptions());
46+
{{/if}}
4247

43-
{{#if (eq payments "polar")}}
44-
const hasProSubscription = customerState?.activeSubscriptions?.length! > 0
45-
console.log("Active subscriptions:", customerState?.activeSubscriptions)
46-
{{/if}}
48+
{{#if (eq payments "polar") }}
49+
const hasProSubscription = (customerState?.activeSubscriptions?.length ?? 0) > 0;
50+
// For debugging: console.log("Active subscriptions:", customerState?.activeSubscriptions);
51+
{{/if}}
4752

48-
return (
49-
<div>
50-
<h1>Dashboard</h1>
51-
<p>Welcome {session.data?.user.name}</p>
52-
{{#if ( or (eq api "orpc") (eq api "trpc"))}}
53-
<p>API: {privateData.data?.message}</p>
54-
{{/if}}
55-
{{#if (eq payments "polar")}}
56-
<p>Plan: {hasProSubscription ? "Pro" : "Free"}</p>
57-
{hasProSubscription ? (
58-
<Button onClick={async () => await authClient.customer.portal()}>
59-
Manage Subscription
60-
</Button>
61-
) : (
62-
<Button onClick={async () => await authClient.checkout({ slug: "pro" })}>
63-
Upgrade to Pro
64-
</Button>
65-
)}
66-
{{/if}}
67-
</div>
68-
);
69-
}
53+
return (
54+
<div>
55+
<h1>Dashboard</h1>
56+
<p>Welcome {session?.user.name}</p>
57+
{{#if (eq api "trpc") }}
58+
<p>API: {privateData.data?.message}</p>
59+
{{else if (eq api "orpc") }}
60+
<p>API: {privateData.data?.message}</p>
61+
{{/if}}
62+
{{#if (eq payments "polar") }}
63+
<p>Plan: {hasProSubscription ? "Pro" : "Free"}</p>
64+
{hasProSubscription ? (
65+
<Button
66+
onClick={async function handlePortal() {
67+
await authClient.customer.portal();
68+
}}
69+
>
70+
Manage Subscription
71+
</Button>
72+
) : (
73+
<Button
74+
onClick={async function handleUpgrade() {
75+
await authClient.checkout({ slug: "pro" });
76+
}}
77+
>
78+
Upgrade to Pro
79+
</Button>
80+
)}
81+
{{/if}}
82+
</div>
83+
);
84+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { authClient } from "@/lib/auth-client";
2+
import { authMiddleware } from "@/middleware/auth";
3+
import { createServerFn } from "@tanstack/react-start";
4+
import { getRequestHeaders } from "@tanstack/react-start/server";
5+
6+
export const getPayment = createServerFn({ method: "GET" })
7+
.middleware([authMiddleware])
8+
.handler(async () => {
9+
const { data: customerState } = await authClient.customer.state({
10+
fetchOptions: {
11+
headers: getRequestHeaders()
12+
}
13+
});
14+
return customerState;
15+
});

0 commit comments

Comments
 (0)