initial commit
This commit is contained in:
12
packages/api/src/context.ts
Normal file
12
packages/api/src/context.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { auth } from "@kk/auth";
|
||||
|
||||
export async function createContext({ req }: { req: Request }) {
|
||||
const session = await auth.api.getSession({
|
||||
headers: req.headers,
|
||||
});
|
||||
return {
|
||||
session,
|
||||
};
|
||||
}
|
||||
|
||||
export type Context = Awaited<ReturnType<typeof createContext>>;
|
||||
20
packages/api/src/index.ts
Normal file
20
packages/api/src/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { ORPCError, os } from "@orpc/server";
|
||||
|
||||
import type { Context } from "./context";
|
||||
|
||||
export const o = os.$context<Context>();
|
||||
|
||||
export const publicProcedure = o;
|
||||
|
||||
const requireAuth = o.middleware(async ({ context, next }) => {
|
||||
if (!context.session?.user) {
|
||||
throw new ORPCError("UNAUTHORIZED");
|
||||
}
|
||||
return next({
|
||||
context: {
|
||||
session: context.session,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
export const protectedProcedure = publicProcedure.use(requireAuth);
|
||||
17
packages/api/src/routers/index.ts
Normal file
17
packages/api/src/routers/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { RouterClient } from "@orpc/server";
|
||||
|
||||
import { protectedProcedure, publicProcedure } from "../index";
|
||||
|
||||
export const appRouter = {
|
||||
healthCheck: publicProcedure.handler(() => {
|
||||
return "OK";
|
||||
}),
|
||||
privateData: protectedProcedure.handler(({ context }) => {
|
||||
return {
|
||||
message: "This is private",
|
||||
user: context.session?.user,
|
||||
};
|
||||
}),
|
||||
};
|
||||
export type AppRouter = typeof appRouter;
|
||||
export type AppRouterClient = RouterClient<typeof appRouter>;
|
||||
Reference in New Issue
Block a user