mirror of
https://github.com/kennethnym/aris.git
synced 2026-06-13 19:11:18 +01:00
chore: rename aelis to freya (#122)
This commit is contained in:
28
apps/freya-backend/src/auth/admin-middleware.ts
Normal file
28
apps/freya-backend/src/auth/admin-middleware.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { Context, MiddlewareHandler, Next } from "hono"
|
||||
|
||||
import type { Auth } from "./index.ts"
|
||||
import type { AuthSessionEnv } from "./session-middleware.ts"
|
||||
|
||||
export type AdminMiddleware = MiddlewareHandler<AuthSessionEnv>
|
||||
|
||||
/**
|
||||
* Creates a middleware that requires a valid session with admin role.
|
||||
* Returns 401 if not authenticated, 403 if not admin.
|
||||
*/
|
||||
export function createRequireAdmin(auth: Auth): AdminMiddleware {
|
||||
return async (c: Context, next: Next): Promise<Response | void> => {
|
||||
const session = await auth.api.getSession({ headers: c.req.raw.headers })
|
||||
|
||||
if (!session) {
|
||||
return c.json({ error: "Unauthorized" }, 401)
|
||||
}
|
||||
|
||||
if (session.user.role !== "admin") {
|
||||
return c.json({ error: "Forbidden" }, 403)
|
||||
}
|
||||
|
||||
c.set("user", session.user)
|
||||
c.set("session", session.session)
|
||||
await next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user