🎉 Unlimited Free KYC - Forever!!

Auth + Data
Engines
NextAuth.js

NextAuth.js Integration

The NextAuth.js integration with Didit provides a streamlined way to add secure human authentication to your Next.js application.

Examples & Resources

Prerequisites

  1. A Next.js application with NextAuth.js installed. If you haven't set up NextAuth.js yet, follow the official guide (opens in a new tab).
  2. A Didit Business Console (opens in a new tab) account.

Set up Didit

  1. Log in to your Didit Business Console (opens in a new tab).
  2. Navigate to Applications in the left sidebar.
  3. Click Create Application.
  4. Enter a name for your application.
  5. For Redirect URI, enter: http://localhost:3000/auth/callback/didit for development, or your production callback URL: https://{YOUR_DOMAIN}/auth/callback/didit.
  6. Click Create.
  7. On the application details page, note the following values - you'll need them for NextAuth.js configuration:
    • Client ID
    • Client Secret

Configure NextAuth.js

  1. Create your NextAuth.js configuration file (e.g., auth.ts):
import NextAuth from "next-auth"
import "next-auth/jwt"
import type { Provider } from "next-auth/providers"
 
 
// Configure Didit provider
const diditProvider: Provider = {
  id: "didit",
  name: "Didit",
  type: "oauth",
  authorization: {
    url: process.env.DIDIT_IS_STAGING === "true"
      ? "https://auth.staging.didit.me"
      : "https://auth.didit.me",
    params: { scope: "openid names document_detail" },
  },
  token: {
    url: process.env.DIDIT_IS_STAGING === "true"
      ? "https://apx.staging.didit.me/auth/v2/token"
      : "https://apx.didit.me/auth/v2/token",
  },
  userinfo: {
    url: process.env.DIDIT_IS_STAGING === "true"
      ? "https://apx.staging.didit.me/auth/v2/users/retrieve/"
      : "https://apx.didit.me/auth/v2/users/retrieve/",
  },
  issuer: process.env.DIDIT_IS_STAGING === "true"
    ? "https://auth.staging.didit.me/"
    : "https://auth.didit.me/",
  clientId: process.env.DIDIT_CLIENT_ID,
  clientSecret: process.env.DIDIT_CLIENT_SECRET,
  checks: ["state", "pkce"],
  profile(profile) {
    return {
      user_data: profile,
      user_id: profile.user_id,
      name: profile.names?.full_name,
      email: profile.email?.email,
      image: profile.picture,
    }
  },
  style: {
    logo: "/didit.png",
  }
}
  1. Add the environment variables to your .env.local file:
AUTH_SECRET=your_nextauth_secret # npx auth secret
DIDIT_CLIENT_ID=your_client_id
DIDIT_CLIENT_SECRET=your_client_secret
DIDIT_IS_STAGING=false
  1. Add the didit provider to your NextAuth configuration list of providers:
export const { handlers, auth, signIn, signOut } = NextAuth({
  debug: !!process.env.AUTH_DEBUG,
  trustHost: true,
  providers: [diditProvider],
  basePath: "/auth",
  session: { strategy: "jwt" },
  callbacks: {
    authorized({ request, auth }) {
      const { pathname } = request.nextUrl
      if (pathname === "/middleware-example") return !!auth
      return true
    },
    jwt({ token, trigger, session }) {
      if (trigger === "update") token.name = session.user.name
      return token
    },
    async session({ session, token }) {
      if (token?.accessToken) session.accessToken = token.accessToken
      return session
    },
  },
})

Features

  • PKCE Flow: Implements secure PKCE (Proof Key for Code Exchange) authentication
  • Staging Support: Configurable staging/production environments
  • Flexible Storage: Uses memory storage for development and Vercel KV for production
  • JWT Sessions: Implements JWT-based session management
  • TypeScript Support: Includes type definitions for enhanced development experience

Troubleshooting

  • Common issues:
    • Ensure your callback URL matches exactly: /auth/callback/didit
    • Verify all environment variables are properly set
    • Confirm the scope values