Here is the code snippet from the middleware file:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// This function can be marked as `async` if using `await` inside
export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const isPublicPath = path === '/login' || path === '/signup';
const token = request.cookies.get('token')?.value || '';
if (isPublicPath && token) {
return NextResponse.redirect(new URL('/', request.nextUrl));
}
if (!isPublicPath && !token) {
return NextResponse.redirect(new URL('/login', request.nextUrl));
}
}
// Check out "Matching Paths" for more information
export const config = {
matcher: ['/', '/profile', 'login', '/signup'],
};
An error occurred:
source
does not start with / for route {"source":"login"}
Error: Invalid middleware detected
Any solutions to this issue?