@didit-protocol/auth package signs people in to your web application with Didit Authentication: the hosted sign-in page, sessions that refresh themselves, sign-out and server-side token verification.
It is published on npm at version 0.3.0 and has no UI.
Use a Sandbox application while you build.
Every Sandbox application has Authentication turned on from the start.
A Live application turns Authentication on from the Products page of the console: see Prepare your application.
Install
/server entry, on Node.js 20 or later and edge runtimes with WebCrypto.
Register your addresses
In the console, under Authentication > Developers, copy your application’s publishable key (pk_test_... for Sandbox, pk_live_... for Live).
Then, under Authentication > Security and sessions > Redirect URLs and allowed origins, register:
A Sandbox application accepts
http:// on localhost, 127.0.0.1 and [::1].
A Live application accepts https:// only.
See redirect URLs for the full rules.
Sign in
auth.ts
sign-in.ts
signIn() opens the hosted page with every method you turned on, including any required second factor.
The sign-in is bound to the browser with PKCE (S256) and a random state.
handleRedirectCallback() refuses a redirect it did not start, exchanges the one-time code and removes it from the address bar.
Call it once per page load.
Options
createDiditAuth(options) takes these options.
Only publishableKey is required.
Anything running on your pages can read web storage, so keep a strict Content Security Policy when you use
"local" or "session".
Read the session and the token
session.ts
A
DiditSession holds userId, sessionId, applicationId, vendorData, email, emailVerified, phone, methods, mfaVerified, verifiedIdentity, organizations and expiresAt.
vendorData is the id that ties the person to their verifications and transactions: see keep one person across products.
Access tokens last five minutes.
There is no public refresh method: getToken() refreshes them for you, once for all the tabs of your origin.
Refresh tokens rotate, and Didit ends a session when a used refresh token is presented again.
Verify the token on your server
A frontend session check only controls what you show. Your backend must verify the caller on every protected request.server.ts
verifyToken accepts only ES256 access tokens (typ: at+jwt, so an ID token is refused) signed by one of your issuer’s published keys, issued by your application and not expired, with 5 seconds of clock tolerance.
It caches the discovery document and keys for ten minutes.
Pass audience to also check aud, or issuer and jwksUri to override what the publishable key implies.
Access tokens stay valid until they expire, so before an action that must see a sign-out or a block at once, also check the session with GET session/.
The tokens and JWKS page has the full token contract, and the server SDKs verify the same tokens in other languages.
More methods
The same client offers headless methods for your own screens (password, emailCode, phone, mfa, protection), passkeys on your application’s Didit sign-in domain, stepUp, reauthenticate and agents.
Every method rejects with a DiditAuthError whose code you can branch on, such as invalid_credentials, rate_limited, invalid_redirect_uri or network_error.
The package README on npm lists them all.
Step-up and reauthentication
stepUp({ action, amount?, currency?, redirectUri? })asks your application whetheractionneeds an identity check.actionispayout,bankorcontact, and a payout needsamount. When one of your step-up rules applies, it navigates to the verification page Didit returns.handleStepUpCallback(url?)runs on the page the person comes back to. It reads?step_up=<id>, removes it from the address bar and reads the outcome from Didit.reauthenticate({ method? })signs the same person in again, on the hosted page (method: "redirect", the default) or with a passkey (method: "passkey", only on your application’s Didit sign-in domain). After the hosted page, finish it withhandleRedirectCallback(). If someone else completes it, it rejects withstep_up_user_mismatchand the original session stays.
{ id, status, approved, rule, method }, where status is not_required, pending, approved, review, declined or expired.
redirectUri must match one of your registered redirect URLs exactly.
Before your backend acts on a step-up, it reads the step-up again with GET step-up/{id}/ and the person’s access token, because a result reported by the browser can be changed.
A headless sign-in can also answer protection_required when your application’s protection rules flag it.
The person then finishes it with the code Didit emailed them (protection.verify({ code })) or with a passkey (protection.passkey()), and methods lists which of the two they can use.
Without the SDK
If you cannot install the package, you can write the hosted flow yourself. It is plain OAuth 2.0 authorization code with PKCE, and this example is the whole contract. Register the exact callback URL and your web origin for your sandbox application. Copy its sign-in origin (https://<slug>.sandbox.auth.didit.me) from the console, as described in the overview. This example uses the browser’s cryptography APIs and requires HTTPS, or localhost in sandbox: see local development for what to register.
The application owns state and the PKCE verifier. Hosted sign-in owns password, email, passkey, phone, SSO and required MFA screens. No API key belongs in this code.
auth.js
signIn() from your sign-in button. On the registered callback page, call handleCallback() once, render a loading state, and show its error if it fails. Use getSession() for the current user and signOut() for logout. The callback removes its code from the address bar before making a request.
This minimal example keeps tokens in memory and signs in again after a page reload or access-token expiry. For persistent sessions, use your backend and secure cookies or implement serialized refresh rotation. Never put refresh tokens in URLs or logs.
If you refresh by hand, replace both tokens together and use only the newest ones: after a refresh the previous access token stops working at GET session/ and DELETE session/. A frontend session check controls presentation only; your backend must validate the caller on every protected request.
Add other products
Your backend verifies the access token (see tokens and JWKS) and reads itsvendor_data. Use that value as vendor_data when creating a verification session or minting a transaction token. A successful sign-in does not imply either product approved the user.
Before shipping, exercise sign-up, returning sign-in, cancellation, mismatched state, reused code, forgot password, MFA, expiry and logout against your enabled sandbox application.