import jwt from "jsonwebtoken";
import { log } from "~/lib/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)
	}
});
