From fa69f3a929181b89b482068b4cba6e1d46c926f9 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 9 Apr 2025 11:38:12 +0800 Subject: [PATCH] fix middle 103 --- bun.lock | 1 + package.json | 1 + src/app/api/middleware/route.ts | 15 +++++++++++++++ src/app/api/validation/route.ts | 7 ++----- src/middleware.ts | 17 +++++++++++++---- x.sh | 5 +++++ 6 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/app/api/middleware/route.ts create mode 100644 x.sh diff --git a/bun.lock b/bun.lock index 770c9c3e..d9dcee17 100644 --- a/bun.lock +++ b/bun.lock @@ -39,6 +39,7 @@ "autoprefixer": "10.4.14", "bufferutil": "^4.0.8", "bun": "^1.1.38", + "colors": "^1.4.0", "date-fns": "^4.1.0", "dayjs": "^1.11.10", "dotenv": "^16.4.5", diff --git a/package.json b/package.json index 293bd2ff..8885411c 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "autoprefixer": "10.4.14", "bufferutil": "^4.0.8", "bun": "^1.1.38", + "colors": "^1.4.0", "date-fns": "^4.1.0", "dayjs": "^1.11.10", "dotenv": "^16.4.5", diff --git a/src/app/api/middleware/route.ts b/src/app/api/middleware/route.ts new file mode 100644 index 00000000..a748a815 --- /dev/null +++ b/src/app/api/middleware/route.ts @@ -0,0 +1,15 @@ +import { cookies } from "next/headers"; +import "colors"; + +export async function GET(req: Request) { + // const token = req.headers.get("Authorizationx")?.split(" ")[1]; + const token = cookies().get("hipmi-key")?.value; + + return new Response(token, { + status: 200, + headers: { + "Content-Type": "application/json", + "Cache-Control": "no-store", + }, + }); +} diff --git a/src/app/api/validation/route.ts b/src/app/api/validation/route.ts index 226de637..ac7000f8 100644 --- a/src/app/api/validation/route.ts +++ b/src/app/api/validation/route.ts @@ -1,11 +1,8 @@ import { NextResponse } from "next/server"; export async function GET(req: Request) { - const auth = req.headers.get("Authorization"); - const token = auth?.split(" ")[1]; - - console.log("Token received in API Validation:", token , `<<<<<<<<<<<<<<<`); - console.log("Authorization header:", auth, `<--------`); + const token = req.headers.get("Authorization")?.split(" ")[1]; + console.log("Token received in API Validation:", token, `<<<<<<<<<<<<<<<`); if (!token) return NextResponse.json({ success: false }, { status: 401 }); diff --git a/src/middleware.ts b/src/middleware.ts index e2cf8f2e..cc97f754 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,5 +1,7 @@ import { jwtVerify } from "jose"; import { NextRequest, NextResponse } from "next/server"; +import { cookies } from "next/headers"; + type MiddlewareConfig = { apiPath: string; @@ -75,6 +77,16 @@ export const middleware = async (req: NextRequest) => { } = middlewareConfig; const { pathname } = req.nextUrl; + // console.warn(cookies().get("hipmi-key")?.value); + // const f = await fetch("https://localhost:3000/api/middleware", { + // method: "GET", + // headers: { + // "Content-Type": "application/json", + // }, + // }); + + // console.warn(await f.text()) + // console.log("Cookies >>", req.cookies.get("hipmi-key")?.value); // Handle CORS const corsResponse = handleCors(req); @@ -92,7 +104,7 @@ export const middleware = async (req: NextRequest) => { const token = getToken(req, sessionKey); const user = await verifyToken({ token, encodedKey }); - console.log("Request URL:", req.url); + console.log("Request URL v2 >>", req.url); // const fetchValidation = async (url: string) => { // try { @@ -151,9 +163,6 @@ export const middleware = async (req: NextRequest) => { } try { - // const validationResponse = await fetchValidation("/api/validation"); - // console.log("Validation Response:", validationResponse); - // console.log("TOKEN >>", token); const validationResponse = await fetch( `${new URL(req.url).origin}/api/validation`, { diff --git a/x.sh b/x.sh new file mode 100644 index 00000000..41fca88d --- /dev/null +++ b/x.sh @@ -0,0 +1,5 @@ +TOKEN="eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wYWUzcmw2MDAwMWZzNWowN3ZtdjRvaCIsIm5vbW9yIjoiNjI4MjM0MDM3NDQxMiIsInVzZXJuYW1lIjoiYmFnYXNfYWRtaW4iLCJhY3RpdmUiOnRydWUsIm1hc3RlclVzZXJSb2xlSWQiOiIzIn0sImlhdCI6MTc0Mjk2OTc1OSwiZXhwIjoxOTYzODcyOTU5fQ.4bFEdhm4TeysoDcC1D54PPbyJPHSMBisiEn5GXoYr30" +# curl -X GET -H "Authorization: Bearer $TOKEN" https://localhost:3000/api/middleware + +curl -X GET -H "Cookie: hipmi-key=$TOKEN; user_id=789" https://localhost:3000/api/middleware +