add auth, drizzle, prisma setup logic with template

This commit is contained in:
Aman Varshney
2025-03-04 09:33:31 +05:30
parent 54d63823bb
commit 792885b9c4
68 changed files with 2692 additions and 921 deletions

View File

@@ -0,0 +1,36 @@
# prod
dist/
# dev
.yarn/
!.yarn/releases
.vscode/*
!.vscode/launch.json
!.vscode/*.code-snippets
.idea/workspace.xml
.idea/usage.statistics.xml
.idea/shelf
# deps
node_modules/
.wrangler
# env
.env
.env.production
.dev.vars
# logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
# misc
.DS_Store
# local db
*.db*

View File

@@ -0,0 +1,34 @@
{
"name": "@better-t/server",
"main": "src/index.ts",
"type": "module",
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"dev:bun": "bun run --hot src/index.ts",
"db:local": "turso dev --db-file local.db",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio",
"check-types": "tsc --noEmit",
"wrangler:dev": "wrangler dev",
"wrangler:deploy": "wrangler deploy --minify",
"compile": "bun build --compile --minify --sourcemap --bytecode ./src/index.ts --outfile server"
},
"dependencies": {
"@hono/node-server": "^1.13.8",
"@hono/trpc-server": "^0.3.4",
"@libsql/client": "^0.14.0",
"@trpc/server": "^11.0.0-rc.748",
"better-auth": "^1.1.16",
"dotenv": "^16.4.7",
"drizzle-orm": "^0.38.4",
"hono": "^4.7.0",
"zod": "^3.24.1"
},
"devDependencies": {
"tsx": "^4.19.2",
"drizzle-kit": "^0.30.4",
"@types/node": "^22.13.4",
"typescript": "^5.7.3"
}
}

View File

@@ -0,0 +1,47 @@
import { serve } from "@hono/node-server";
import { trpcServer } from "@hono/trpc-server";
import "dotenv/config";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
import { auth } from "./lib/auth";
import { createContext } from "./lib/context";
import { appRouter } from "./routers/index";
const app = new Hono();
app.use(logger());
app.use(
"/*",
cors({
origin: process.env.CORS_ORIGIN!,
allowMethods: ["GET", "POST", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
credentials: true,
}),
);
app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));
app.use(
"/trpc/*",
trpcServer({
router: appRouter,
createContext: (_opts, hono) => {
return createContext({ hono });
},
}),
);
app.get("/healthCheck", (c) => {
return c.text("OK");
});
const port = 3000;
console.log(`Server is running on http://localhost:${port}`);
serve({
fetch: app.fetch,
port,
});

View File

@@ -0,0 +1,18 @@
import type { Context as HonoContext } from "hono";
import { auth } from "./auth";
export type CreateContextOptions = {
hono: HonoContext;
};
export async function createContext({ hono }: CreateContextOptions) {
const session = await auth.api.getSession({
headers: hono.req.raw.headers,
});
return {
session,
};
}
export type Context = Awaited<ReturnType<typeof createContext>>;

View File

@@ -0,0 +1,24 @@
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;
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,
},
});
});

View File

@@ -0,0 +1,15 @@
import { router, publicProcedure, protectedProcedure } from "../lib/trpc";
export const appRouter = router({
healthCheck: publicProcedure.query(() => {
return "OK";
}),
privateData: protectedProcedure.query(({ ctx }) => {
return {
message: "This is private",
user: ctx.session.user,
};
}),
});
export type AppRouter = typeof appRouter;

View File

@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"esModuleInterop": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist",
"types": ["node"],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
}
}