feat: midterm shit done

This commit is contained in:
Tony Yang
2025-04-15 03:59:33 +08:00
parent f093df29a1
commit f7ee02586b
34 changed files with 1460 additions and 197 deletions
+26
View File
@@ -0,0 +1,26 @@
import * as jose from 'jose';
import { createErrorResponse } from "../utils";
export async function verifyJWT(context) {
const { request, env } = context;
// Check for a valid JWT token
const authHeader = request.headers.get("Authorization");
if (!authHeader) {
return createErrorResponse("Missing Authorization header", 401);
}
const token = authHeader.split(" ")[1];
try {
// Verify the token
const { payload, protectedHeader } = await jose.jwtVerify(token, new TextEncoder().encode(env.JWT_SECRET), {
issuer: 'urn:example:issuer',
audience: 'urn:example:audience',
});
context.user = { userId: payload.id, username: payload.username };
return; // Continue to the next middleware or function
} catch (error) {
return createErrorResponse("Invalid or expired token", 401);
}
}