simple-fs-server/routes/_middleware.ts
2023-01-14 03:03:58 +09:00

25 lines
630 B
TypeScript

import {
HandlerContext,
MiddlewareHandlerContext,
Status,
} from "$fresh/server.ts";
import { getCookies } from "http/cookie.ts";
import { verify } from "djwt";
import { prepareSecretKey } from "../util/secret.ts";
export const handler = async (
req: Request,
ctx: MiddlewareHandlerContext<Record<string, unknown>>,
) => {
const secret_key = await prepareSecretKey();
const cookies = getCookies(req.headers);
const jwt = cookies["auth"];
try {
const payload = await verify(jwt, secret_key);
ctx.state["login"] = payload;
} catch (_) {
ctx.state["login"] = null;
}
return await ctx.next();
};