2023-10-10 21:56:25 -04:00
|
|
|
import jwt from "jsonwebtoken";
|
2023-12-30 17:36:22 -05:00
|
|
|
import { log } from "@server/logger";
|
2023-12-31 14:23:02 -05:00
|
|
|
import { messages } from "@server/constants";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { User } from "@models/user";
|
2023-10-10 21:56:25 -04:00
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
2023-12-29 20:11:07 -05:00
|
|
|
let ahead = (getHeaders(event).authorization || getCookie(event, "auth:token") || "")?.replace("Bearer ", "");
|
2023-12-30 17:36:22 -05:00
|
|
|
// console.log("in here fucknuts", ahead);
|
|
|
|
// log.debug(`'${ahead}'`, { label: "idk" });
|
2023-10-10 21:56:25 -04:00
|
|
|
if (ahead) {
|
2023-12-30 17:36:22 -05:00
|
|
|
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)
|
2023-10-10 21:56:25 -04:00
|
|
|
}
|
|
|
|
});
|