diff options
Diffstat (limited to 'packages/api/middlewares/auth.ts')
| -rw-r--r-- | packages/api/middlewares/auth.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/packages/api/middlewares/auth.ts b/packages/api/middlewares/auth.ts new file mode 100644 index 00000000..7f39a6f9 --- /dev/null +++ b/packages/api/middlewares/auth.ts @@ -0,0 +1,22 @@ +import { createMiddleware } from "hono/factory"; +import { HTTPException } from "hono/http-exception"; + +import { AuthedContext, createCallerFactory } from "@karakeep/trpc"; +import { appRouter } from "@karakeep/trpc/routers/_app"; + +const createCaller = createCallerFactory(appRouter); + +export const authMiddleware = createMiddleware<{ + Variables: { + ctx: AuthedContext; + api: ReturnType<typeof createCaller>; + }; +}>(async (c, next) => { + if (!c.var.ctx || !c.var.ctx.user || c.var.ctx.user === null) { + throw new HTTPException(401, { + message: "Unauthorized", + }); + } + c.set("api", createCaller(c.get("ctx"))); + await next(); +}); |
