refactor(server/middleware): rename currentUser
middleware
This commit is contained in:
parent
6afc547f8e
commit
d67530eb64
23
server/middleware/05.currentUser.ts
Normal file
23
server/middleware/05.currentUser.ts
Normal file
@ -0,0 +1,23 @@
|
||||
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)
|
||||
}
|
||||
});
|
@ -1,15 +0,0 @@
|
||||
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;
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user