This commit is contained in:
Your Name committed 2026-06-13 22:02:04 -04:00
1 parent 20f43bd58d
commit 1ac727f4e5
10 files changed
+306 -122

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import { Elysia } from "elysia";
import { jwt } from "@elysiajs/jwt";
export const adminOnly = new Elysia({ name: "adminOnly" })
.use(
jwt({
name: "jwt",
secret: process.env.JWT_SECRET || "fallback-secret"
})
)
.onBeforeHandle(async ({ jwt, headers, set }) => {
const auth = headers.authorization;
if (!auth?.startsWith("Bearer ")) {
set.status = 401;
return { error: "Unauthorized: Missing or invalid token format" };
}
const token = auth.substring(7);
const payload = await jwt.verify(token);
if (!payload) {
set.status = 401;
return { error: "Unauthorized: Invalid token" };
}
if ((payload as any).role !== "ADMIN") {
set.status = 401;
return { error: "Unauthorized: Admin access required" };
}
});