next/server/middleware/05.currentUser.ts

21 lines
715 B
TypeScript

import jwt from "jsonwebtoken";
import { log } from "@server/logger";
import { messages } from "@server/constants";
import { User } from "@models/user";
export default defineEventHandler(async (event) => {
let ahead = (getHeaders(event).authorization || getCookie(event, "auth:token") || "")?.replace("Bearer ", "");
// console.log("in here fucknuts", ahead);
// log.debug(`'${ahead}'`, { label: "idk" });
if (ahead) {
let toktok = jwt.verify(
ahead,
// ahead.replace("Bearer ", ""),
useRuntimeConfig().jwt,
) as jwt.JwtPayload;
let user = await User.findById(toktok.id as number).exec();
if (user && toktok) event.context.currentUser = user;
// setCookie(event, "auth:token", ahead)
}
});