Skip to main content
Didit’s @didit-protocol/auth-nextjs package adds Authentication to the Next.js App Router. People sign in on your hosted Didit sign-in page, the session lives in HttpOnly cookies, and server components, route handlers and server actions read it with auth(). It is published on npm at version 0.1.0 and needs Next.js 15.5 or later (or 16) and React 19.
Use a Sandbox application while you build. Every Sandbox application has Authentication turned on from the start. A Live application needs access from Didit first: see Prepare your application.

Install and configure

.env.local
The publishable key names your application’s sign-in domain, so there is no other address to set. A value that is not a Didit key fails at startup with DiditAuthConfigError. The package also reads DIDIT_SESSION_CACHE_SECONDS (default 30, 0 checks the session with Didit every time). Register the addresses in Authentication > Security and sessions > Redirect URLs and allowed origins: A Sandbox application accepts http:// on localhost. A Live application accepts https:// only.

Set up the proxy, layout and pages

proxy.ts
app/layout.tsx
app/sign-in/page.tsx
<SignUp /> works the same way on app/sign-up/page.tsx. Both open the hosted method chooser, where Create account starts registration, and send someone already signed in straight on to redirect_url. diditAuthProxy options are protect (paths that need a signed-in person), signInUrl (default /auth/sign-in) and afterSignOutUrl (default /). The proxy serves the /auth/* routes for you, so you add no route files: When the access token has expired, the proxy rotates the refresh token first, so pages render with a valid session.

Read the session on the server

app/account/page.tsx
app/api/me/route.ts
auth() returns { session, userId, sessionId, getToken, getStepUp }. It checks the session with Didit, so a session revoked from the console or from another device stops working within DIDIT_SESSION_CACHE_SECONDS. auth.protect() returns the session, or sends a signed-out visitor to sign in and back to the page they asked for. The proxy must run on the route, so an expired access token is refreshed first. Do not rely on the proxy alone: call auth() at each data access and mutation.

Client components

SignedIn, SignedOut, UserButton, ConnectedAgents, useAuth() and useUser() are those of @didit-protocol/auth-react, re-exported. Under <DiditAuth> they render signed in or out from the first server render.
app/header.tsx

Call another backend with the access token

useAuth().getToken() in the browser, or getToken() from auth() on the server, returns the person’s short-lived access token. A backend that receives it verifies it with verifyToken from @didit-protocol/auth/server: see the JavaScript quickstart.
After a refresh, the previous access token stops working at GET session/ and DELETE session/. Always take the token from getToken() right before the request.

Step-up before sensitive actions

Didit returns the person to DIDIT_REDIRECT_URI, and the proxy brings them back to the page that asked, with only the step-up’s id in the address. What runs in a browser can be changed, so read the step-up again on the server before you act:

Security notes

  • The access and refresh tokens are in HttpOnly, SameSite=Lax cookies. Over HTTPS they are Secure and __Host- prefixed.
  • Each sign-in keeps its own PKCE verifier and state in a ten-minute cookie, and the callback refuses a state it did not issue.
  • redirect_url only accepts paths in your app.
  • Sign-out, step-up, the code exchange and agent revocation only accept requests from your own origin.
  • A refresh token is spent once per server process. Apps on several server instances can still race on a refresh at the same moment, and the session then ends and the person signs in again.

Connect the other products

Create verification sessions and mint scoped monitoring tokens on the server, and use the person’s vendorData as their vendor_data. Return only a verification session URL or short-lived SDK token to the browser, and keep DIDIT_API_KEY out of public environment variables. Use the web verification SDK for verification and the transaction submission guide for monitoring. See tokens and JWKS for the token contract.
Last modified on November 11, 2026