import { NextRequest, NextResponse } from "next/server"; import { verifyToken } from…
- User
import { NextRequest, NextResponse } from "next/server"; import { verifyToken } from "@/src/app/(folders)/helper/verifyToken"; import { publicRoutes } from "@/src/app/(folders)/constants/middleware";
export default async function middleware(request: NextRequest) { const pathname = request.nextUrl.pathname; console.log("Middleware executing for path:", pathname); // Check if the current route is public if (publicRoutes(pathname)) { return NextResponse.next(); }
// For private routes, check the token const token = request.headers.get("authorization")?.split(" ")[1] || "";
if (!token) { return NextResponse.redirect(new URL("/login", request.url)); }
try { const decodedToken = verifyToken(token);
code// Check if token is valid and not expired if (!decodedToken) { return NextResponse.redirect(new URL("/login", request.url)); } // Token is valid, proceed to the requested page return NextResponse.next();} catch (error) { console.error("Token verification failed:", error); return NextResponse.redirect(new URL("/login", request.url)); } }
export const config = { matcher: [ "/((?!api|_next/static|_next/image|favicon\.ico|src/app/\(.\)).))", ], }; C:\Users\farzad\AppData\Local\pnpm\pnpm.exe run dev
phoenix-next-app@0.1.0 dev E:\BT-Projects\phoenix-next-app next dev
▲ Next.js 14.2.14
- Local: http://localhost:3000
- Environments: .env.local
- Experiments (use with caution): · optimizeCss · adjustFontFallbacks · instrumentationHook · ...
✓ Starting... ✓ Ready in 2.9s ○ Compiling / ... ✓ Compiled / in 28.4s (2123 modules) GET / 200 in 29969ms ○ Compiling /api/home ... ✓ Compiled /api/home in 1059ms (1039 modules) GET /api/home 200 in 1218ms
i cant see any logs from the middleware in my nextjs app
- Assistant
- User
why this is now working?: import { NextRequest, NextResponse } from "next/server"; import { publicRoutes } from "@/src/app/(folders)/constants/middleware"; import { verifyToken } from "@/src/app/(folders)/helper/verifyToken";
export default async function middleware(request: NextRequest) { try { const pathname = request.nextUrl.pathname; console.log("Middleware executing for path:", pathname);
codeif (publicRoutes(pathname)) { console.log("Path is public, continuing..."); return NextResponse.next(); } const token = request.headers.get("authorization")?.split(" ")[1] || ""; console.log("Token extracted:", token); if (!token) { console.log("No token found, redirecting to login..."); return NextResponse.redirect(new URL("/login", request.url)); } const decodedToken = verifyToken(token); if (!decodedToken) { console.log("Invalid token, redirecting to login..."); return NextResponse.redirect(new URL("/login", request.url)); } console.log("Token verified, allowing access to protected route"); return NextResponse.next(); - Assistant
The middleware you've written seems syntactically correct and generally follows the pattern of what you'd expect in a Next.js middleware function. However, there are a few considerations and potential reasons why it might not be working:
- User
now its running on all paths i want it to be running only page.tsx files inside src/app/(pages)
- Assistant
To configure your middleware to run only on specific paths, such as on
page.tsxfiles insidesrc/app/(pages), you need to use the configuration option effectively. The property specifies patterns that determine which requests the middleware should apply to. - User
Error parsing
/src/app/(pages)/**/pagehttps://nextjs.org/docs/messages/invalid-route-source Reason: Unexpected MODIFIER at 17, expected END/src/app/(pages)/**/page ^
sourceparse failed for route {"source":"/src/app/(pages)/**/page"}Error: Invalid middleware found
Process finished with exit code 0
- Assistant