From d67530eb6480da4eb4d0ec7754c3fe6ff6bf0077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Tue, 10 Oct 2023 21:56:25 -0400 Subject: [PATCH] refactor(server/middleware): rename `currentUser` middleware --- server/middleware/05.currentUser.ts | 23 +++++++++++++++++++++++ server/middleware/currentUser.ts | 15 --------------- 2 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 server/middleware/05.currentUser.ts delete mode 100644 server/middleware/currentUser.ts diff --git a/server/middleware/05.currentUser.ts b/server/middleware/05.currentUser.ts new file mode 100644 index 0000000..c7d22be --- /dev/null +++ b/server/middleware/05.currentUser.ts @@ -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) + } +}); diff --git a/server/middleware/currentUser.ts b/server/middleware/currentUser.ts deleted file mode 100644 index bba409c..0000000 --- a/server/middleware/currentUser.ts +++ /dev/null @@ -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; - } -});