MFA recovery codes — generate, show, store
One-time codes the user needs to reach if they lose their authenticator.
Recovery codes are returned exactly once — at generation time. After that, only their hash is stored. The UX is to show the code prominently and tell the user to save it somewhere safe.
Server-side: generation
The library's MfaService.generateRecoveryCode(userId) returns:
Expose an endpoint:
Client-side: show once
Using the recovery code
When the user lost their TOTP device, they call client.resetMfa(code):
How many to give
The library generates one at a time. Some apps prefer a stack of 10 — call generateRecoveryCode ten times in a loop, store all ten on the client side, and let the user use them one at a time.
(Note: each call returns one code; the user holds the list. Re-running this invalidates the previous batch — only the most recent set is valid server-side.)
Don't email recovery codes
Tempting, but it defeats the purpose — if their MFA is compromised, their email might be too. Show, copy, done. Optionally print to PDF.
Related
- MFA.