mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat: add clerk auth support with convex (#548)
This commit is contained in:
@@ -3,7 +3,7 @@ import { createRouter as createTanStackRouter } from "@tanstack/react-router";
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
import { routerWithQueryClient } from "@tanstack/react-router-with-query";
|
||||
import { ConvexQueryClient } from "@convex-dev/react-query";
|
||||
import { ConvexProvider } from "convex/react";
|
||||
import { ConvexProvider, ConvexReactClient } from "convex/react";
|
||||
import { routeTree } from "./routeTree.gen";
|
||||
import Loader from "./components/loader";
|
||||
import "./index.css";
|
||||
@@ -31,7 +31,11 @@ export function createRouter() {
|
||||
if (!CONVEX_URL) {
|
||||
console.error("missing envar VITE_CONVEX_URL");
|
||||
}
|
||||
const convexQueryClient = new ConvexQueryClient(CONVEX_URL);
|
||||
const convex = new ConvexReactClient(CONVEX_URL, {
|
||||
unsavedChangesWarning: false,
|
||||
});
|
||||
|
||||
const convexQueryClient = new ConvexQueryClient(convex);
|
||||
|
||||
const queryClient: QueryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
@@ -49,7 +53,7 @@ export function createRouter() {
|
||||
defaultPreload: "intent",
|
||||
defaultPendingComponent: () => <Loader />,
|
||||
defaultNotFoundComponent: () => <div>Not Found</div>,
|
||||
context: { queryClient },
|
||||
context: { queryClient, convexClient: convex, convexQueryClient },
|
||||
Wrap: ({ children }) => (
|
||||
<ConvexProvider client={convexQueryClient.convexClient}>
|
||||
{children}
|
||||
@@ -82,7 +86,7 @@ const trpcClient = createTRPCClient<AppRouter>({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `${import.meta.env.VITE_SERVER_URL}/trpc`,
|
||||
{{#if auth}}
|
||||
{{#if (eq auth "better-auth")}}
|
||||
fetch(url, options) {
|
||||
return fetch(url, {
|
||||
...options,
|
||||
|
||||
@@ -8,22 +8,37 @@ import {
|
||||
Scripts,
|
||||
createRootRouteWithContext,
|
||||
useRouterState,
|
||||
useRouteContext,
|
||||
} from "@tanstack/react-router";
|
||||
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
|
||||
import Header from "../components/header";
|
||||
import appCss from "../index.css?url";
|
||||
{{#if (eq backend "convex")}}
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
{{else}}
|
||||
{{#unless (eq api "none")}}
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
{{/unless}}
|
||||
import type { ConvexQueryClient } from "@convex-dev/react-query";
|
||||
import type { ConvexReactClient } from "convex/react";
|
||||
{{/if}}
|
||||
import Loader from "@/components/loader";
|
||||
|
||||
{{#if (and (eq backend "convex") (eq auth "clerk"))}}
|
||||
import { ClerkProvider, useAuth } from "@clerk/tanstack-react-start";
|
||||
import { getAuth } from "@clerk/tanstack-react-start/server";
|
||||
import { createServerFn } from "@tanstack/react-start";
|
||||
import { getWebRequest } from "@tanstack/react-start/server";
|
||||
import { ConvexProviderWithClerk } from "convex/react-clerk";
|
||||
|
||||
const fetchClerkAuth = createServerFn({ method: "GET" }).handler(async () => {
|
||||
const auth = await getAuth(getWebRequest());
|
||||
const token = await auth.getToken({ template: "convex" });
|
||||
return { userId: auth.userId, token };
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq backend "convex")}}
|
||||
export interface RouterAppContext {
|
||||
queryClient: QueryClient;
|
||||
convexClient: ConvexReactClient;
|
||||
convexQueryClient: ConvexQueryClient;
|
||||
}
|
||||
{{else}}
|
||||
{{#if (eq api "trpc")}}
|
||||
@@ -68,11 +83,42 @@ export const Route = createRootRouteWithContext<RouterAppContext>()({
|
||||
}),
|
||||
|
||||
component: RootDocument,
|
||||
{{#if (and (eq backend "convex") (eq auth "clerk"))}}
|
||||
beforeLoad: async (ctx) => {
|
||||
const { userId, token } = await fetchClerkAuth();
|
||||
if (token) {
|
||||
ctx.context.convexQueryClient.serverHttpClient?.setAuth(token);
|
||||
}
|
||||
return { userId, token };
|
||||
},
|
||||
{{/if}}
|
||||
});
|
||||
|
||||
function RootDocument() {
|
||||
const isFetching = useRouterState({ select: (s) => s.isLoading });
|
||||
|
||||
{{#if (and (eq backend "convex") (eq auth "clerk"))}}
|
||||
const context = useRouteContext({ from: Route.id });
|
||||
return (
|
||||
<ClerkProvider>
|
||||
<ConvexProviderWithClerk client={context.convexClient} useAuth={useAuth}>
|
||||
<html lang="en" className="dark">
|
||||
<head>
|
||||
<HeadContent />
|
||||
</head>
|
||||
<body>
|
||||
<div className="grid h-svh grid-rows-[auto_1fr]">
|
||||
<Header />
|
||||
{isFetching ? <Loader /> : <Outlet />}
|
||||
</div>
|
||||
<Toaster richColors />
|
||||
<TanStackRouterDevtools position="bottom-left" />
|
||||
<Scripts />
|
||||
</body>
|
||||
</html>
|
||||
</ConvexProviderWithClerk>
|
||||
</ClerkProvider>
|
||||
);
|
||||
{{else}}
|
||||
return (
|
||||
<html lang="en" className="dark">
|
||||
<head>
|
||||
@@ -94,4 +140,5 @@ function RootDocument() {
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
{{/if}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user