TouchSession: ((...args) => Promise<void>)

Type declaration

    • (...args): Promise<void>
    • Touch the session object. If rolling sessions are enabled and autoSave is disabled, you will need to call this method to update the session expiry.

      In the App Router:

      In a route handler:

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

      export async function GET() {
      await touchSession();
      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 `getSession`.
      import { touchSession, withApiAuthRequired } from '@auth0/nextjs-auth0';

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

      export { GET };

      In a page or React Server Component:

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

      export default async function MyPage({ params, searchParams }) {
      await touchSession();
      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 touch 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 }) {
      await touchSession();
      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 { touchSession } from '@auth0/nextjs-auth0';

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

      In a page:

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

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

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

      In middleware:

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

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

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

      @category Server

      Parameters

      • Rest ...args: [IncomingMessage, ServerResponse] | [NextApiRequest, NextApiResponse] | [NextRequest, NextResponse] | []

      Returns Promise<void>