fix middle 104
This commit is contained in:
@@ -1,15 +1,30 @@
|
|||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import "colors";
|
import "colors";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(req: Request) {
|
export async function GET(req: Request) {
|
||||||
// const token = req.headers.get("Authorizationx")?.split(" ")[1];
|
// const token = req.headers.get("Authorizationx")?.split(" ")[1];
|
||||||
const token = cookies().get("hipmi-key")?.value;
|
const SESSIONKEY = process.env.NEXT_PUBLIC_BASE_SESSION_KEY!;
|
||||||
|
console.log(
|
||||||
|
"Token received in API Middleware:",
|
||||||
|
SESSIONKEY,
|
||||||
|
`<<<<<<<<<<<<<<<`
|
||||||
|
);
|
||||||
|
const tokenCookies = cookies().get(SESSIONKEY)?.value;
|
||||||
|
console.log("Token received in Cookies:", tokenCookies);
|
||||||
|
|
||||||
return new Response(token, {
|
const tokenHeader = req.headers.get("Authorization")?.split(" ")[1];
|
||||||
status: 200,
|
console.log("Token received in Header:", tokenHeader);
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Cache-Control": "no-store",
|
if (!tokenCookies) return NextResponse.json({ success: false });
|
||||||
},
|
return NextResponse.json({ success: true });
|
||||||
});
|
|
||||||
|
// return new Response(token, {
|
||||||
|
// status: 200,
|
||||||
|
// headers: {
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// "Cache-Control": "no-store",
|
||||||
|
// },
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { jwtVerify } from "jose";
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
|
|
||||||
|
|
||||||
type MiddlewareConfig = {
|
type MiddlewareConfig = {
|
||||||
apiPath: string;
|
apiPath: string;
|
||||||
loginPath: string;
|
loginPath: string;
|
||||||
@@ -163,28 +162,30 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const validationResponse = await fetch(
|
const originURL = new URL(req.url).origin;
|
||||||
`${new URL(req.url).origin}/api/validation`,
|
console.log("Origin URL >> ", originURL);
|
||||||
{
|
const pathApiValidation = `${new URL(req.url).origin}/api/validation`;
|
||||||
|
const validationResponse = await fetch(pathApiValidation, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!validationResponse.ok) {
|
||||||
|
console.error("Validation failed:", validationResponse.statusText);
|
||||||
|
return setCorsHeaders(unauthorizedResponseAPI());
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
const validationResponseJson = await validationResponse.json();
|
const validationResponseJson = await validationResponse.json();
|
||||||
console.log("Validation Response JSON:", validationResponseJson);
|
|
||||||
|
|
||||||
if (validationResponseJson.success === false) {
|
if (validationResponseJson.success === false) {
|
||||||
return setCorsHeaders(unauthorizedResponseDataUserNotFound(req));
|
return setCorsHeaders(unauthorizedResponseDataUserNotFound(req));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validationResponse.ok) {
|
|
||||||
return setCorsHeaders(unauthorizedResponseAPI());
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error validating API request:", error);
|
console.error(
|
||||||
|
"Error validating API request:",
|
||||||
|
(error as Error).message || error
|
||||||
|
);
|
||||||
return setCorsHeaders(unauthorizedResponseValidationAPIRequest());
|
return setCorsHeaders(unauthorizedResponseValidationAPIRequest());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user