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