• Get the user's session from the server.

    In the App Router:

    In a route handler:

    // app/my-api/route.js
    import { getSession } from '@auth0/nextjs-auth0';

    export async function GET() {
    const { user } = await getSession();
    return NextResponse.json({ foo: 'bar' });
    }

    // Or, it's slightly more efficient to use the `req`, `res` args if you're
    // using another part of the SDK like `withApiAuthRequired` or `getAccessToken`.
    import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';

    const GET = withApiAuthRequired(async function GET(req) {
    const res = new NextResponse();
    const { user } = await getSession(req, res);
    return NextResponse.json({ foo: 'bar' }, res);
    });

    export { GET };

    In a page or React Server Component:

    // app/my-page/page.js
    import { getSession } from '@auth0/nextjs-auth0';

    export default async function MyPage({ params, searchParams }) {
    const { user } = await getSession();
    return <h1>My Page</h1>;
    }

    Note: You can't write to the cookie in a React Server Component, so updates to the session like setting the expiry of the rolling session won't be persisted. For this, we recommend interacting with the session in the middleware.

    You can also get the session in a page or route in the Edge Runtime:

    // app/my-api/route.js
    import { getSession } from '@auth0/nextjs-auth0/edge'; // Note the /edge import

    export default async function MyPage({ params, searchParams }) {
    const { user } = await getSession();
    return <h1>My Page</h1>;
    }

    export const runtime = 'edge';

    Note: The Edge runtime features are only supported in the App Router.

    In the Page Router:

    In an API handler:

    // pages/api/my-api.js
    import { getSession } from '@auth0/nextjs-auth0';

    export default async function MyApi(req, res) {
    const { user } = await getSession(req, res);
    res.status(200).json({ name: user.name });
    }

    In a page:

    // pages/my-page.js
    import { getSession } from '@auth0/nextjs-auth0';

    export default function About() {
    return <div>About</div>;
    }

    export async function getServerSideProps(ctx) {
    const { user } = await getSession(ctx.req, ctx.res);
    return { props: { foo: 'bar' } };
    }

    In middleware:

    import { NextResponse } from 'next/server';
    import { getSession } from '@auth0/nextjs-auth0/edge'; // Note the /edge import

    export async function middleware(req) {
    const res = new NextResponse();
    const { user } = await getSession(req, res);
    return NextResponse.redirect(new URL('/bar', request.url), res);
    }

    export const config = {
    matcher: '/foo',
    };

    @category Server

    Parameters

    • Rest ...args: [] | [IncomingMessage, ServerResponse<IncomingMessage>] | [NextApiRequest, NextApiResponse<any>] | [NextRequest, NextResponse<unknown>]

    Returns Promise<undefined | null | default>