simple-fs-server/routes/_middleware.ts

21 lines
622 B
TypeScript
Raw Normal View History

2023-01-14 03:03:22 +09:00
import { HandlerContext, MiddlewareHandlerContext, Status } from "$fresh/server.ts";
2023-01-05 18:18:07 +09:00
import { getCookies } from "http/cookie.ts";
2023-01-06 18:24:27 +09:00
import { verify } from "djwt";
2023-01-05 18:18:07 +09:00
import { prepareSecretKey } from "../util/secret.ts";
2023-01-06 18:24:27 +09:00
export const handler = async (
req: Request,
ctx: MiddlewareHandlerContext<Record<string, unknown>>,
) => {
2023-01-06 23:22:00 +09:00
const secret_key = await prepareSecretKey();
2023-01-06 18:24:27 +09:00
const cookies = getCookies(req.headers);
const jwt = cookies["auth"];
try {
const payload = await verify(jwt, secret_key);
ctx.state["login"] = payload;
2023-01-14 03:03:22 +09:00
} catch (_) {
2023-01-06 18:24:27 +09:00
ctx.state["login"] = null;
}
return await ctx.next();
2023-01-14 03:03:22 +09:00
}