// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { verify } from 'jsonwebtoken';
// Simple middleware to protect specific routes
export function middleware(request: NextRequest) {
// Get token from cookies
const token = request.cookies.get('token')?.value;
// If no token exists, direct the user to the main page.
// Send a response redirect
if (!token) {
return NextResponse.redirect(new URL('/login', request.url));
}
try {
// Verify JWT
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';
verify(token, JWT_SECRET);
// If the token is valid, transfer control of request to the appropriate route handler function
return NextResponse.next();
}
catch (error) {
// Token is invalid, redirect to login
return NextResponse.redirect(new URL('/login', request.url));
}
}
// Only run middleware on protected routes
export const config = {
matcher: ['/dashboard/:path*', '/profile/:path*', '/api/protected/:path*'],
};