feat: add clerk auth support with convex (#548)

This commit is contained in:
Aman Varshney
2025-08-29 00:21:08 +05:30
committed by GitHub
parent 8d48ae0359
commit 54bcdf1cbc
153 changed files with 1954 additions and 771 deletions

View File

@@ -3,7 +3,7 @@ import { RPCLink } from "@orpc/client/fetch";
import { createTanstackQueryUtils } from "@orpc/tanstack-query";
import { QueryCache, QueryClient } from "@tanstack/react-query";
import type { AppRouterClient } from "../../server/src/routers";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { authClient } from "@/lib/auth-client";
{{/if}}
@@ -17,7 +17,7 @@ export const queryClient = new QueryClient({
export const link = new RPCLink({
url: `${process.env.EXPO_PUBLIC_SERVER_URL}/rpc`,
{{#if auth}}
{{#if (eq auth "better-auth")}}
headers() {
const headers = new Map<string, string>();
const cookies = authClient.getCookie();

View File

@@ -1,11 +1,11 @@
{{#if (eq backend 'next')}}
import type { NextRequest } from "next/server";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
export async function createContext(req: NextRequest) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: req.headers,
});
@@ -19,7 +19,7 @@ export async function createContext(req: NextRequest) {
{{else if (eq backend 'hono')}}
import type { Context as HonoContext } from "hono";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
@@ -28,7 +28,7 @@ export type CreateContextOptions = {
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: context.req.raw.headers,
});
@@ -45,7 +45,7 @@ export async function createContext({ context }: CreateContextOptions) {
{{else if (eq backend 'elysia')}}
import type { Context as ElysiaContext } from "elysia";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
@@ -54,7 +54,7 @@ export type CreateContextOptions = {
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: context.request.headers,
});
@@ -70,13 +70,13 @@ export async function createContext({ context }: CreateContextOptions) {
}
{{else if (eq backend 'express')}}
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext(opts: any) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers),
});
@@ -93,13 +93,13 @@ export async function createContext(opts: any) {
{{else if (eq backend 'fastify')}}
import type { IncomingHttpHeaders } from "node:http";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext(req: IncomingHttpHeaders) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(req),
});

View File

@@ -5,7 +5,7 @@ export const o = os.$context<Context>();
export const publicProcedure = o;
{{#if auth}}
{{#if (eq auth "better-auth")}}
const requireAuth = o.middleware(async ({ context, next }) => {
if (!context.session?.user) {
throw new ORPCError("UNAUTHORIZED");

View File

@@ -1,4 +1,4 @@
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { createContext } from '@/lib/context'
{{/if}}
import { appRouter } from '@/routers'
@@ -10,7 +10,7 @@ const handler = new RPCHandler(appRouter)
async function handleRequest(req: NextRequest) {
const { response } = await handler.handle(req, {
prefix: '/rpc',
context: {{#if auth}}await createContext(req){{else}}{}{{/if}},
context: {{#if (eq auth "better-auth")}}await createContext(req){{else}}{}{{/if}},
})
return response ?? new Response('Not found', { status: 404 })

View File

@@ -12,7 +12,7 @@ export default defineNuxtPlugin(() => {
const rpcLink = new RPCLink({
url: rpcUrl,
{{#if auth}}
{{#if (eq auth "better-auth")}}
fetch(url, options) {
return fetch(url, {
...options,

View File

@@ -26,7 +26,7 @@ export const link = new RPCLink({
{{else}}
url: `${import.meta.env.VITE_SERVER_URL}/rpc`,
{{/if}}
{{#if auth}}
{{#if (eq auth "better-auth")}}
fetch(url, options) {
return fetch(url, {
...options,

View File

@@ -14,7 +14,7 @@ export const queryClient = new QueryClient({
export const link = new RPCLink({
url: `${import.meta.env.VITE_SERVER_URL}/rpc`,
{{#if auth}}
{{#if (eq auth "better-auth")}}
fetch(url, options) {
return fetch(url, {
...options,

View File

@@ -15,7 +15,7 @@ export const queryClient = new QueryClient({
export const link = new RPCLink({
url: `${PUBLIC_SERVER_URL}/rpc`,
{{#if auth}}
{{#if (eq auth "better-auth")}}
fetch(url, options) {
return fetch(url, {
...options,

View File

@@ -1,4 +1,4 @@
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { authClient } from "@/lib/auth-client";
{{/if}}
import { QueryClient } from "@tanstack/react-query";
@@ -12,7 +12,7 @@ const trpcClient = createTRPCClient<AppRouter>({
links: [
httpBatchLink({
url: `${process.env.EXPO_PUBLIC_SERVER_URL}/trpc`,
{{#if auth}}
{{#if (eq auth "better-auth")}}
headers() {
const headers = new Map<string, string>();
const cookies = authClient.getCookie();

View File

@@ -1,11 +1,11 @@
{{#if (eq backend 'next')}}
import type { NextRequest } from "next/server";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
export async function createContext(req: NextRequest) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: req.headers,
});
@@ -22,7 +22,7 @@ export async function createContext(req: NextRequest) {
{{else if (eq backend 'hono')}}
import type { Context as HonoContext } from "hono";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
@@ -31,7 +31,7 @@ export type CreateContextOptions = {
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: context.req.raw.headers,
});
@@ -48,7 +48,7 @@ export async function createContext({ context }: CreateContextOptions) {
{{else if (eq backend 'elysia')}}
import type { Context as ElysiaContext } from "elysia";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { auth } from "./auth";
{{/if}}
@@ -57,7 +57,7 @@ export type CreateContextOptions = {
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: context.request.headers,
});
@@ -74,13 +74,13 @@ export async function createContext({ context }: CreateContextOptions) {
{{else if (eq backend 'express')}}
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext(opts: CreateExpressContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers),
});
@@ -97,13 +97,13 @@ export async function createContext(opts: CreateExpressContextOptions) {
{{else if (eq backend 'fastify')}}
import type { CreateFastifyContextOptions } from "@trpc/server/adapters/fastify";
{{#if auth}}
{{#if (eq auth "better-auth")}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext({ req, res }: CreateFastifyContextOptions) {
{{#if auth}}
{{#if (eq auth "better-auth")}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(req.headers),
});

View File

@@ -7,7 +7,7 @@ export const router = t.router;
export const publicProcedure = t.procedure;
{{#if auth}}
{{#if (eq auth "better-auth")}}
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session) {
throw new TRPCError({

View File

@@ -28,7 +28,7 @@ const trpcClient = createTRPCClient<AppRouter>({
{{else}}
url: `${import.meta.env.VITE_SERVER_URL}/trpc`,
{{/if}}
{{#if auth}}
{{#if (eq auth "better-auth")}}
fetch(url, options) {
return fetch(url, {
...options,
@@ -78,7 +78,7 @@ export 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,