Use built-in next-auth middleware. Login page redirect should be handled in getServerSideProps
import type { JWT } from "next-auth/jwt";
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
// `withAuth` augments your `Request` with the user's token.