.env variable undefined in middleware
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
hello, i have an issue when importing an environement variable in my middleware to verify my cookie token. The .env variable is set to undefined and i really don't know why. this is my middleware code
import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
const JWT_SECRET = process.env.JWT_SECRET;
console.log(JWT_SECRET);
const JWTSECRET = new TextEncoder().encode(JWT_SECRET);
async function verifyToken(token: string) {
try {
const { payload } = await jwtVerify(token, JWTSECRET, {
algorithms: ["HS256"],
});
return payload;
} catch (error) {
console.error("Token verification failed:", error);
return null;
}
}
export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
if (url.pathname.startsWith("/dashboard")) {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.redirect(new URL("/home/login", request.url));
}
const payload = await verifyToken(token);
if (!payload) {
return NextResponse.redirect(new URL("/home/login", request.url));
}
}
if (url.pathname.startsWith("/home/find-chef")) {
const response = NextResponse.next();
response.headers.set("x-url", url.toString());
return response;
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/home/find-chef/:path*"],
};1 Reply
Burmese
is this during local dev or deployed?