simple-fs-server/routes/_middleware.ts

20 lines
629 B
TypeScript
Raw Normal View History

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