fix middle 103
This commit is contained in:
1
bun.lock
1
bun.lock
@@ -39,6 +39,7 @@
|
|||||||
"autoprefixer": "10.4.14",
|
"autoprefixer": "10.4.14",
|
||||||
"bufferutil": "^4.0.8",
|
"bufferutil": "^4.0.8",
|
||||||
"bun": "^1.1.38",
|
"bun": "^1.1.38",
|
||||||
|
"colors": "^1.4.0",
|
||||||
"date-fns": "^4.1.0",
|
"date-fns": "^4.1.0",
|
||||||
"dayjs": "^1.11.10",
|
"dayjs": "^1.11.10",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
|
|||||||
@@ -50,6 +50,7 @@
|
|||||||
"autoprefixer": "10.4.14",
|
"autoprefixer": "10.4.14",
|
||||||
"bufferutil": "^4.0.8",
|
"bufferutil": "^4.0.8",
|
||||||
"bun": "^1.1.38",
|
"bun": "^1.1.38",
|
||||||
|
"colors": "^1.4.0",
|
||||||
"date-fns": "^4.1.0",
|
"date-fns": "^4.1.0",
|
||||||
"dayjs": "^1.11.10",
|
"dayjs": "^1.11.10",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
|
|||||||
15
src/app/api/middleware/route.ts
Normal file
15
src/app/api/middleware/route.ts
Normal file
@@ -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",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(req: Request) {
|
export async function GET(req: Request) {
|
||||||
const auth = req.headers.get("Authorization");
|
const token = req.headers.get("Authorization")?.split(" ")[1];
|
||||||
const token = auth?.split(" ")[1];
|
console.log("Token received in API Validation:", token, `<<<<<<<<<<<<<<<`);
|
||||||
|
|
||||||
console.log("Token received in API Validation:", token , `<<<<<<<<<<<<<<<`);
|
|
||||||
console.log("Authorization header:", auth, `<--------`);
|
|
||||||
|
|
||||||
if (!token) return NextResponse.json({ success: false }, { status: 401 });
|
if (!token) return NextResponse.json({ success: false }, { status: 401 });
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { jwtVerify } from "jose";
|
import { jwtVerify } from "jose";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { cookies } from "next/headers";
|
||||||
|
|
||||||
|
|
||||||
type MiddlewareConfig = {
|
type MiddlewareConfig = {
|
||||||
apiPath: string;
|
apiPath: string;
|
||||||
@@ -75,6 +77,16 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
} = middlewareConfig;
|
} = middlewareConfig;
|
||||||
|
|
||||||
const { pathname } = req.nextUrl;
|
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
|
// Handle CORS
|
||||||
const corsResponse = handleCors(req);
|
const corsResponse = handleCors(req);
|
||||||
@@ -92,7 +104,7 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
const token = getToken(req, sessionKey);
|
const token = getToken(req, sessionKey);
|
||||||
const user = await verifyToken({ token, encodedKey });
|
const user = await verifyToken({ token, encodedKey });
|
||||||
|
|
||||||
console.log("Request URL:", req.url);
|
console.log("Request URL v2 >>", req.url);
|
||||||
|
|
||||||
// const fetchValidation = async (url: string) => {
|
// const fetchValidation = async (url: string) => {
|
||||||
// try {
|
// try {
|
||||||
@@ -151,9 +163,6 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const validationResponse = await fetchValidation("/api/validation");
|
|
||||||
// console.log("Validation Response:", validationResponse);
|
|
||||||
// console.log("TOKEN >>", token);
|
|
||||||
const validationResponse = await fetch(
|
const validationResponse = await fetch(
|
||||||
`${new URL(req.url).origin}/api/validation`,
|
`${new URL(req.url).origin}/api/validation`,
|
||||||
{
|
{
|
||||||
|
|||||||
5
x.sh
Normal file
5
x.sh
Normal file
@@ -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
|
||||||
|
|
||||||
Reference in New Issue
Block a user