next/server/middleware/currentUser.ts

20 lines
616 B
TypeScript
Raw Normal View History

import jwt from "jsonwebtoken";
import { User } from "~/models/user";
export default defineEventHandler(async (event) => {
let ahead = (getHeaders(event).authorization || "")?.replace("Bearer ", "");
console.log(`=------------------------${event.path}`);
console.log("h", getHeaders(event));
console.log(JSON.stringify(ahead));
if (ahead) {
console.log("ahead", 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;
}
});