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