import jwt from "jsonwebtoken";
import { User } from "~/models/user";

export default defineEventHandler(async (event) => {
	let ahead = (getHeaders(event).authorization || "")?.replace("Bearer ", "");
	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) event.context.currentUser = user;
	}
});
