LiveAutosaved · just now
Refactor auth middleware
v3 · draftSystem
Prompt
{{ file }}{{ style }}
Output · streaming
418 tokens·1.2s
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@
-export function withAuth(handler) {
- return async (req, res) => {
- if (!req.headers.authorization) return res.status(401).end();
- ...
- }
-}
+export const requireAuth = createMiddleware(async (ctx) => {
+ const token = readBearer(ctx.request)
+ if (!token) throw new HttpError(401, "Missing bearer")
+ ctx.user = await verify(token)
+})