mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add auth, drizzle, prisma setup logic with template
This commit is contained in:
47
apps/cli/template/base/packages/server/src/index.ts
Normal file
47
apps/cli/template/base/packages/server/src/index.ts
Normal 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,
|
||||
});
|
||||
18
apps/cli/template/base/packages/server/src/lib/context.ts
Normal file
18
apps/cli/template/base/packages/server/src/lib/context.ts
Normal 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>>;
|
||||
24
apps/cli/template/base/packages/server/src/lib/trpc.ts
Normal file
24
apps/cli/template/base/packages/server/src/lib/trpc.ts
Normal 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,
|
||||
},
|
||||
});
|
||||
});
|
||||
15
apps/cli/template/base/packages/server/src/routers/index.ts
Normal file
15
apps/cli/template/base/packages/server/src/routers/index.ts
Normal 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;
|
||||
Reference in New Issue
Block a user