This commit is contained in:
Aman Varshney
2025-04-14 21:45:28 +05:30
parent 8b03441909
commit 7f441ef670
268 changed files with 3513 additions and 3039 deletions

View File

@@ -0,0 +1,108 @@
{{#if (eq backend 'next')}}
import type { NextRequest } from "next/server";
{{#if auth}}
import { auth } from "./auth";
{{/if}}
export async function createContext(req: NextRequest) {
{{#if auth}}
const session = await auth.api.getSession({
headers: req.headers,
});
return {
session,
};
{{else}}
// No auth configured
return {
session: null,
};
{{/if}}
}
{{else if (eq backend 'hono')}}
import type { Context as HonoContext } from "hono";
{{#if auth}}
import { auth } from "./auth";
{{/if}}
export type CreateContextOptions = {
context: HonoContext;
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
const session = await auth.api.getSession({
headers: context.req.raw.headers,
});
return {
session,
};
{{else}}
// No auth configured
return {
session: null,
};
{{/if}}
}
{{else if (eq backend 'elysia')}}
import type { Context as ElysiaContext } from "elysia";
{{#if auth}}
import { auth } from "./auth";
{{/if}}
export type CreateContextOptions = {
context: ElysiaContext;
};
export async function createContext({ context }: CreateContextOptions) {
{{#if auth}}
const session = await auth.api.getSession({
headers: context.request.headers,
});
return {
session,
};
{{else}}
// No auth configured
return {
session: null,
};
{{/if}}
}
{{else if (eq backend 'express')}}
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
{{#if auth}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext(opts: CreateExpressContextOptions) {
{{#if auth}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers),
});
return {
session,
};
{{else}}
// No auth configured
return {
session: null,
};
{{/if}}
}
{{else}}
// Default or fallback context if backend is not recognized or none
// This might need adjustment based on your default behavior
export async function createContext() {
return {
session: null,
};
}
{{/if}}
export type Context = Awaited<ReturnType<typeof createContext>>;

View File

@@ -0,0 +1,26 @@
import { initTRPC, TRPCError } from "@trpc/server";
import type { Context } from "./context";
export const t = initTRPC.context<Context>().create();
export const router = t.router;
export const publicProcedure = t.procedure;
{{#if auth}}
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Authentication required",
cause: "No session",
});
}
return next({
ctx: {
...ctx,
session: ctx.session,
},
});
});
{{/if}}