Verify a code at sign-in
The user needs a confirmed authenticator before they can sign in with a TOTP code.
After the first factor succeeds, keep a short-lived sign-in attempt on your backend. Associate it with the account and the selected authenticator. If the user can choose among multiple authenticators, validate their selection against that account’s records before saving it to the attempt. Grant full session access only after verification succeeds for that attempt.
const verified = await passlock.verifyTotp({ userId: signInAttempt.userId, authenticatorId: signInAttempt.authenticatorId, code,});
if (verified.success) { // Complete the sign-in attempt and issue your application's session.}verified.value contains userId, authenticatorId, and verifiedAt in Unix milliseconds. Your backend uses that result directly to finish sign-in or authorize the pending action. Keep the Passlock API key on the backend and have the browser submit the code to your application.
Handle verification failures
Section titled “Handle verification failures”Branch on result.failure and result.error._tag for expected errors. Handle thrown transport or service failures separately and leave the sign-in attempt incomplete.
| Error tag | Application response |
|---|---|
@error/InvalidTotpCode |
Ask for the current code. |
@error/TotpCodeAlreadyUsed |
Ask the user to wait for the next code. |
@error/TotpRateLimited |
Wait at least retryAfterSeconds before another attempt. |
@error/TotpEnrollmentExpired |
Start a new enrollment. |
@error/TotpAuthenticatorNotActive |
Guide the user through another enrolled method or your account recovery flow. |
@error/TotpAuthenticatorLimit |
Use kind (pending or active) and limit to tell the user which limit they reached. |
Passlock prevents reuse of an accepted time step and throttles attempts. Keep codes out of application logs and send only the exact digits shown in the authenticator app.