Enroll an authenticator app
Use the Passlock Server SDK from your backend to create and confirm an enrollment.
Start enrollment
Section titled “Start enrollment”Resolve userId from the user’s session using your stable application account ID. issuer is the application name shown in the authenticator app, username is the account label shown there, and displayName is an optional nickname you can show in your own settings.
const created = await passlock.createTotpAuthenticator({ userId: account.id, issuer: "Acme", username: account.email, displayName: "Personal phone",});Check created.success before accessing created.value. Save created.value.authenticator.id against the account in server-side enrollment state. Send created.value.setup.qrCodeDataUrl to the enrolling user and render it as an image for them to scan. This is a base64 encoded PNG data URL. Offer setup.secret for manual entry if they cannot scan the QR code. setup.uri is the equivalent otpauth:// link.
All three setup values contain the shared secret. Deliver setup only to the authorized user over HTTPS, set Cache-Control: no-store on your setup response, and keep setup values out of logs, analytics, and third-party QR services. Passlock returns setup values only at creation. If the user loses them, start a new enrollment and delete the abandoned pending authenticator if you still have its ID. Here is an example:
const created = await passlock.createTotpAuthenticator({ userId: account.id, issuer: "Acme", username: account.email, displayName: "Personal phone",});
// we don't want secrets hanging aroundconst headers = { "Cache-Control": "no-store" };
if (created.failure) { // handle the failures const error = created.error;
if (error._tag === "@error/TotpRateLimited") { return Response.json( { message: "Too many attempts. Try again later." }, { status: 429, headers: { ...headers, "Retry-After": String(error.retryAfterSeconds) } }, ); }
if (error._tag === "@error/TotpAuthenticatorLimit") { return Response.json( { message: "You've reached the authenticator limit." }, { status: 409, headers }, ); }
return Response.json( { message: "Could not start authenticator setup." }, { status: 502, headers }, );}
const { authenticator, setup } = created.value;
// link the authenticator id to the user's// account but mark it as pending enrollmentawait savePendingEnrollment(account.id, { authenticatorId: authenticator.id, expiresAt: authenticator.expiresAt,});
// qrCodeDataUrl will be a base64 encoded data url (png)return Response.json( { qrCodeDataUrl: setup.qrCodeDataUrl, secret: setup.secret }, { status: 201, headers },);Confirm enrollment
Section titled “Confirm enrollment”Ask the user for the first code their app displays. Read userId and authenticatorId from the authenticated account and its pending enrollment state. Submit the code as a string so leading zeros survive.
const confirmed = await passlock.confirmTotpAuthenticator({ userId: account.id, authenticatorId: enrollment.authenticatorId, code,});
if (confirmed.success) { // Record the enrollment as complete in your application.}Confirmation activates the credential and consumes the accepted code. The user needs a fresh code for the next verification. Pending enrollments expire after 15 minutes by default. Use created.value.authenticator.expiresAt for the actual deadline and start a new enrollment after expiry.
Once enrollment is confirmed, you can verify a code at sign-in.