Skip to content

Quickstart

Create a realm and a client in the console, then sign a user in to a Next.js app with Auth.js — in about ten minutes.

This guide takes you from an empty account to a Next.js app (App Router) that signs people in through Authio’s hosted login and reads who they are on the server. It uses Auth.js v5 and @hyper.authio/provider-nextauth, the same provider the Authio console itself signs in with.

Before you start

You need Node.js 20.19 or later and a Next.js app using the App Router. If you do not have an Authio account yet, sign up: the 14-day trial needs no card.

Set up Authio

  1. Create a realm

    In the console, open Realms and choose New realm. Give it a name; the realm ID follows the name until you type one of your own, becomes part of the realm’s issuer and cannot be changed later, so pick something stable such as acme. Creating the realm opens its page.

    The Summary at the top of the realm’s page shows its issuer, the address every client of the realm points at:

    IssuerPlain text
    https://authio.api.hyperio.tech/realms/acme
  2. Create a client for your app

    Open Clients and choose New client. In Where it lives, pick the realm you just created, and under Kind of application choose Web application: it keeps a secret and starts with the authorization code and refresh token grants, with PKCE S256. Give the client a Display name — the name your users see on the sign-in screen — and a Client ID (lowercase letters, numbers and hyphens, for example acme-web), which cannot be changed later. Sender email, the address the client’s mail comes from, is required too.

    Under Redirect URIs, add the address Auth.js receives the sign-in on:

    Redirect URIPlain text
    http://localhost:3000/api/auth/callback/authio
    The client secret is shown once, in a dialog right after the client is created. Copy it now; if you lose it, choose Revoke and generate new secret in the Security section of the client’s page.
  3. Add a user who may sign in

    A new client lets in only the accounts linked to it: the ones created through it or granted access to it. On the client’s page, go to the Users section, choose New user, fill in the account in the side panel and save. The user is created in the realm already linked to the client.

    To let every account of the realm in, turn off Restrict to linked accounts under Who may sign in, in the client’s Configuration section. An account is then linked on its first sign-in.

Connect your app

  1. Install the packages

    Auth.js v5 is still in beta and breaks between beta numbers, so pin the one the provider is tested against.

    TerminalBash
    npm install @hyper.authio/provider-nextauth next-auth@5.0.0-beta.32
  2. Add your credentials

    Auth.js reads the provider’s settings from variables prefixed with AUTH_AUTHIO_. AUTH_SECRET is a different thing: the key that encrypts the session cookie. Generate it with npx auth secret and never reuse the client secret for it. AUTH_URL is where the app is served from.

    .env.local.env
    AUTH_SECRET=generated-by-npx-auth-secret
    AUTH_AUTHIO_ISSUER=https://authio.api.hyperio.tech/realms/acme
    AUTH_AUTHIO_ID=acme-web
    AUTH_AUTHIO_SECRET=YOUR_CLIENT_SECRET
    AUTH_URL=http://localhost:3000
  3. Configure Auth.js

    Create the Authio integration with createAuthio and hand its provider, callbacks and events to Auth.js: the provider reads the variables above, and the callbacks renew the access token before it expires and put the person’s roles and permissions in the session. checks mirrors the client, which requires PKCE. Then expose Auth.js’s route handlers.

    auth.tsTypeScript
    import NextAuth from 'next-auth'
    import { createAuthio } from '@hyper.authio/provider-nextauth/v5'
    
    // Reads AUTH_AUTHIO_ISSUER, AUTH_AUTHIO_ID and AUTH_AUTHIO_SECRET by itself.
    // checks mirrors the client: a new client requires PKCE with S256.
    export const authio = createAuthio({ checks: ['pkce', 'state', 'nonce'] })
    
    export const { handlers, signIn, signOut, auth } = NextAuth({
      providers: [authio.provider],
      callbacks: authio.callbacks,
      events: authio.events,
    })
  4. Add sign-in and sign-out

    Read the session on the server with auth() and use server actions for the two buttons:

    app/page.tsxTypeScript
    import { auth, signIn, signOut } from '@/auth'
    
    export default async function Home() {
      const session = await auth()
    
      if (!session?.user) {
        return (
          <form
            action={async () => {
              'use server'
              await signIn('authio')
            }}
          >
            <button type="submit">Sign in</button>
          </form>
        )
      }
    
      return (
        <form
          action={async () => {
            'use server'
            await signOut()
          }}
        >
          <p>Signed in as {session.user.email}</p>
          <button type="submit">Sign out</button>
        </form>
      )
    }
  5. Try it

    Run npm run dev, open http://localhost:3000 and choose Sign in. You land on the realm’s hosted login. Sign in with the user you created on the client’s page, and you are back in your app with a session. The client’s page also has an integration guide with this code already filled in with its values, and it marks its last step done once the client receives its first real token.

Troubleshooting

What you seeWhyWhat to do
The hosted login refuses the request for its redirect URIThe address is compared in full with the ones registered on the client.Register exactly http://localhost:3000/api/auth/callback/authio — scheme, host, port and path.
The hosted login refuses the credentials of a user who exists in the realmThe client lets in only linked accounts, and this one is not linked to it. The answer does not say which, so it reads like a wrong password.Create the user from the client’s Users section, or turn off Restrict to linked accounts in the client’s Configuration.
invalid_scope — scope must include openidThe client requires the openid scope and the request did not carry it.If you override scope, write the whole list: openid profile email plus what you add.
UntrustedHost in productionAuth.js only trusts the host named in AUTH_URL (or AUTH_TRUST_HOST).Set AUTH_URL to your app’s public address.
An error page right after a long pause on the sign-in screenThe check Auth.js keeps for one sign-in expired or was replaced by a newer one in another tab.Start the sign-in again; Authio already holds the session, so it goes straight through.

Next steps