src > middleware.ts import { NextRequest } from "next/server"; import { NextResponse } from "next/server"; export function middleware(request: NextRequest){ // getting the current path const path = request.nextUrl.pathname; // set public path const publicPath = path === '/login' || path === '/signup'; // set token const token = request.cookies.get('token')?.value || ''; // check if user is logged in if(publicPath && token){ return NextResponse.redirect(new URL('/profile', request.nextUrl)); } // check if user is logged out if(!publicPath && !token){ return NextResponse.redirect(new URL('/login', request.nextUrl)); } } export const config = { matcher: [ '/', '/profile', '/profile/:path*', '/login', '/signup' ] }