Checking for the existing of the appSession cookie like corysimmons described works for routing use cases, if you want your middleware to securely check if the user is logged in you could try this:
import { getSession } from '@auth0/nextjs-auth0/edge';
import { NextRequest, NextResponse } from 'next/server'
export default async function middleware(req: NextRequest) {
const response = NextResponse.next();
const session = await getSession(req, response);
if (req.nextUrl.pathname === '/' && session?.user) {
return NextResponse.redirect(new URL('/app', req.url))
}
if (req.nextUrl.pathname.startsWith('/app') && !session?.user) {
return NextResponse.redirect(new URL('/', req.url))
}
return NextResponse.next();
}
The trick is to import getSession from the edge package. This gives you more flexibility in case you need to combine multiple middlewares like described here. NextJs NestedMiddleware
Using withMiddlewareAuthRequired from the auth0 package, does not allow you to use other middlewares as far as I could tell.