Test Next.js sign-in with Auth.js

Run an Auth.js sign-in flow with a public OpenID Connect client, predictable test users and server-side role checks.

Verified with Next.js 16.4.6, Auth.js 5.0.0-beta.32.

The problem

Testing server-side authorization in a Next.js app needs more than a button that changes after sign-in. You need known identities and roles to check what the server returns for a direct request to a protected page.

Why HeyIdentity fits

A Generic configuration issues real ID tokens for the admin and reader you select in the picker. Auth.js uses S256 PKCE, validates the callback and maps the ID-token roles into its session. The session JSON exposes the roles and user details. Auth.js stores the ID token in the encrypted, HTTP-only session cookie and reads it server-side for sign-out.

Prerequisites

Install Node.js 22.12 or later and Git.

Clone the verified example

Start in the directory where you keep your repositories. These commands open the revision used for this guide. Browse the runnable example on GitHub.

git clone https://github.com/heyidentity/examples.git heyidentity-examples
cd heyidentity-examples
git checkout --detach 391c07e010699fff660fa3b4c9f4ce86cd6fa234

Set up HeyIdentity

Reuse an active configuration if it uses the Generic profile and the issuer, client and registered URLs match this example. Register http://localhost:5173/api/auth/callback/heyidentity for sign-in and http://localhost:5173/ for sign-out. Stop the previous app before running this one, or use an unused APP_PORT and register the corresponding URLs.

For a different profile, incompatible settings or an expired configuration, start from a separate checkout so the old .env and agent token remain available. Temporary setup asks you to accept the Terms of Use. An unclaimed configuration expires after seven days. Claim or recover a setup.

Use a temporary configuration

Provision a Generic configuration from the checkout root.

node setup/provision.mjs --temporary

Use a configuration you own

Copy .env.example to .env at the checkout root. Under Connect your app, copy the issuer to OIDC_AUTHORITY. It has the shape https://app.heyidentity.dev/t/{tenantRouteId}/c/{configurationId}. Copy the public client ID from Clients to OIDC_CLIENT_ID. Register the exact callback and sign-out URLs above. Set ADMIN_USER_KEY and READER_USER_KEY to the keys of two test users with roles of ["admin"] and ["reader"] respectively.

Generate the Auth.js session secret

With the root settings ready, run this once from the checkout root. AUTH_SECRET protects Auth.js sessions and is separate from the OIDC client configuration. Keep it on the server and in the ignored .env.

node -e "require('node:fs').appendFileSync('.env', '\nAUTH_SECRET=' + require('node:crypto').randomBytes(32).toString('base64url') + '\n')"

Configure the provider

The example reads authority and clientId from the server’s settings and keeps session configuration separate from the provider entry.

    providers: [{
      id: 'heyidentity',
      name: 'HeyIdentity',
      type: 'oidc',
      issuer: authority,
      clientId,
      client: { token_endpoint_auth_method: 'none' },
      checks: ['pkce', 'state'],
      authorization: { params: { scope: 'openid profile' } },
    }],

Run and verify

Install the dependencies and start Next.js from the checkout root.

npm --prefix apps/nextjs ci
npm --prefix apps/nextjs run dev

Open http://localhost:5173/ and sign in as the admin. Open /admin and verify that the admin area appears. Sign out and repeat with the reader. The page should show the reader’s identity and Access denied. Signing out returns to the exact registered root.

For the shared browser suite, stop the manually started app and add these settings to the root .env. They start this app, check its callback path and tell the offline callback check that Auth.js reports its own error codes.

EXAMPLE_APP_COMMAND="npm --prefix apps/nextjs run dev"
EXAMPLE_OIDC_CALLBACK_PATH=/api/auth/callback/heyidentity
EXAMPLE_CALLBACK_ERRORS=normalized

Run the test commands from the checkout root.

cd playwright/typescript
npm ci
npx playwright install chromium
npx playwright test

Limitations

The example uses the verified Auth.js v5 beta release, a local HTTP callback and the Generic profile. Use a separate session secret for each checkout. HeyIdentity is for development and testing.

Set up your own test configuration

Sign up

Try the sign-in flow in the demo