next/server/middleware/05.currentUser.ts

24 lines
678 B
TypeScript
Raw Normal View History

import jwt from "jsonwebtoken";
import { log } from "@server/logger";
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)
}
});