Add login to a Next.js app with SyAuth

Doha I. · Published · Lire en français

Add login to a Next.js app with SyAuth

Register a public client in SyAuth with the callback http://127.0.0.1:3000/auth/callback, download the SyAuth sign-in starter, set APP_URL, SYAUTH_CLIENT_ID and SESSION_SECRET in .env.local, and run npm run dev:next. You get sign-in, consent, a validated callback, a protected page and logout, built on OpenID Connect with S256 PKCE.

This guide adds a complete, production-shaped sign-in to a Next.js app with SyAuth. It uses the official starter from the SyAuth docs, which also runs on plain Node.js and Cloudflare Workers with the same code.

You will end up with five working steps: sign-in → consent → callback → protected page → logout.

What you need

  • A free SyAuth account (create one).
  • Node.js 22.12 or later and npm.
  • About ten minutes.

How the flow works

The starter uses OpenID Connect, the identity layer on top of OAuth 2.0:

  1. Your app redirects the user to SyAuth's hosted login with a PKCE code challenge (S256), a state and a nonce.
  2. The user signs in and approves the consent screen.
  3. SyAuth redirects back to /auth/callback with a one-time code.
  4. Your server exchanges the code with the original PKCE verifier and validates the signed ID token: signature against the JWKS keys, issuer, audience, expiry, state and nonce.
  5. Your app creates its own encrypted, HttpOnly session cookie.

The starter uses the maintained openid-client library for these checks, so you do not write the cryptography yourself.

1. Register the application

In the SyAuth dashboard, finish account, workspace and application setup. For this starter:

  • Client type: Public (PKCE protects the code exchange, no secret needed).
  • Grant types: authorization_code,refresh_token.
  • Response type: code.
  • Scopes: openid profile email write.
  • Callback URL: http://127.0.0.1:3000/auth/callback for local development.

Register the callback exactly. localhost and 127.0.0.1 are different hosts for OAuth.

2. Download and configure

Download the starter, extract it and open a terminal in the syauth-sign-in-starter folder:

npm ci
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Copy .env.example to .env.local and set:

APP_URL=http://127.0.0.1:3000
SYAUTH_ISSUER=https://api.syauth.com/e/v1
SYAUTH_CLIENT_ID=YOUR_PUBLIC_CLIENT_ID
SESSION_SECRET=THE_RANDOM_VALUE_YOU_GENERATED

The discovery document is derived from the issuer: https://api.syauth.com/e/v1/.well-known/openid-configuration. Never prefix secrets with NEXT_PUBLIC_, and never commit .env.local.

3. Run it

npm run dev:next

Open http://127.0.0.1:3000. A catch-all Route Handler serves the starter pages.

4. Verify the whole flow

  1. Click Sign in. Always start from your site, never from an old authorization URL.
  2. Sign in as an application user of the client's workspace. This is not your dashboard account.
  3. Approve the consent screen.
  4. You come back to /auth/callback; the starter validates everything and sets the session cookie.
  5. Open the Protected page: it shows the signed-in identity. Without a session, it redirects to sign-in.
  6. Click Sign out of this website. The POST is protected against cross-site requests and clears the session.

Deploying

Run npm run build:next and deploy with your usual Next.js hosting. Set the same environment variables in your host, set APP_URL to your HTTPS origin, and register the HTTPS callback, for example https://your-app.example/auth/callback.

Common errors

Error Fix
Redirect URI rejected Register the exact callback, including scheme, host and port.
LOGIN_ATTEMPT_EXPIRED Start again from your site's Sign in link; an attempt lasts 10 minutes.
OAUTH_VALIDATION Check the callback, client settings and issuer, then start a fresh attempt.
APP_ORIGIN APP_URL must match the exact origin you opened.

Going further

  • Add social login with Google, GitHub, LinkedIn or Facebook from the dashboard, without changing your code.
  • Add MFA or passkeys for your users.
  • Building a browser-only React SPA or a mobile app? Use a public client with PKCE and follow the OAuth and OIDC guide.

The full reference for this starter, including the Node.js and Workers adapters, is in the SyAuth docs.

Free for up to 30,000 registered users

Start on the free plan and upgrade when your project grows.

Add login to a Next.js app with SyAuth | SyAuth