1+ import { getUser } from "@/functions/get-user";
2+ {{ #if (eq payments " polar" ) }}
13import { Button } from "@/components/ui/button";
24import { authClient } from "@/lib/auth-client";
3- {{ #if (eq api " trpc" )}}
5+ import { getPayment } from "@/functions/get-payment";
6+ {{ /if }}
7+ {{ #if (eq api " trpc" ) }}
48import { useTRPC } from "@/utils/trpc";
59import { useQuery } from "@tanstack/react-query";
610{{ /if }}
7- {{ #if (eq api " orpc" )}}
11+ {{ #if (eq api " orpc" ) }}
812import { orpc } from "@/utils/orpc";
913import { useQuery } from "@tanstack/react-query";
1014{{ /if }}
1115import { createFileRoute, redirect } from "@tanstack/react-router";
1216
1317export 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
3237function 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+ }
0 commit comments