I need want to send the only authenticated user to the /chat route in next js how to do ut using the middleware? Although I created a middleware and place it inside the root folder it dosen't detect the api call any one who use /chat can go to the chat page?[folder structure]
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const token = request.cookies.get('token')?.value;
console.log("hi there from middleware");
console.log('Middleware - Token:', token );
// if user is not logged in and visiting /chat
if (!token && request.nextUrl.pathname.startsWith('/chat')) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/chat/:path*'],
}