Backup Codes
Backup codes are one-use recovery proofs for eligible first-party password sign-in after the required primary credential. They reduce recovery risk but do not guarantee account access or prevent lockout.
What Are Backup Codes?
When classic-factor enrollment creates the first stored method row, or push enrollment finds no usable recovery set, UniAuth may issue 8 backup codes. Each is an uppercase hexadecimal XXXX-XXXX-XXXX-XXXX value that can replace the second-factor proof once after the required primary credential.
UniAuth returns plaintext codes only in the response that issues or regenerates them, then stores only keyed HMAC-SHA256 values. Save the returned set immediately because the original values cannot be recovered.
Generating Backup Codes
Classic-factor verification may return codes when it creates the account's first stored method row. Push enrollment may issue them when no usable set exists. An eligible signed-in account can replace its current set from security settings after fresh step-up.
- Navigate toAccount → Security.
- Complete eligible classic-factor verification or configured push enrollment; initial issuance remains conditional on the existing factor and recovery state.
- When codes are issued, data.backupCodes contains exactly 8 formatted one-use values.
- Copy or download them immediately — they will not be shown again.
The classic-factor verification response includes data.backupCodes only when that enrollment creates the first stored method row:
POST /api/auth/2fa/verify
Content-Type: application/json
Cookie: auth_token=...
{
"code": "123456",
"method": "totp"
}
// Response (200), when this enrollment creates the first stored method row:
{
"success": true,
"message": "2FA method enabled successfully",
"data": {
"backupCodes": [
"A1B2-C3D4-E5F6-0718",
"192A-3B4C-5D6E-7F80",
"89AB-CDEF-0123-4567",
"FEDC-BA98-7654-3210",
"0A1B-2C3D-4E5F-6071",
"8192-A3B4-C5D6-E7F8",
"1357-9BDF-2468-ACE0",
"C0DE-FEED-5AFE-B00C"
],
"isFirstMethod": true
}
}Important: Each plaintext set is returned only in its issuance or regeneration response. UniAuth stores only a keyed HMAC-SHA256 value for each code and cannot recover the original values.
Storing Backup Codes Safely
Backup codes are only useful if you can find them when you need them. Follow these best practices:
- Password manager — Store the codes as a secure note in your password manager (1Password, Bitwarden, etc.). This is the most convenient option for most users.
- Print a hard copy — Print the codes and store the paper in a secure location such as a safe or locked drawer.
- Encrypted file — Save the codes in an encrypted file on a separate device or USB drive.
- Do not store backup codes in plain text on your computer, in email drafts, or in cloud notes without encryption.
Tip: Store your backup codes separately from your 2FA device. If your phone is lost or stolen, you do not want the attacker to also have your backup codes.
Using a Backup Code
On an eligible first-party password login, a backup code can replace the second-factor proof after the required email and password.
- Enter your email and password as usual.
- When the MFA prompt appears, enter one formatted backup code and select the backup method.
- ClickVerify.
- The server atomically claims a valid code. Continue only when every admission check succeeds and the final response reports data.authenticated === true; the claimed code cannot be reused.
For first-party password login, submit a backup code as twoFactorCode with email, password and twoFactorMethod: backup to /api/auth/login. A backup code does not replace the password or confirm enrollment.
POST /api/auth/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "your-password",
"twoFactorCode": "A1B2-C3D4-E5F6-0718",
"twoFactorMethod": "backup"
}Each HMAC-protected code is removed atomically from the stored set when claimed, so concurrent reuse fails. Security settings can report how many unused codes remain.
Regenerating Backup Codes
If you have used most of your backup codes or suspect they have been compromised, you should regenerate them. Regeneration creates a fresh set of 8 codes and immediately invalidates all previous codes.
- Navigate toAccount → Security.
- In the Two-Factor Authentication section, click Regenerate Backup Codes.
- Confirm the action. All existing codes are revoked.
- Save the new codes immediately using the best practices above.
Warning: Regenerating codes is irreversible. Any previously saved codes will stop working immediately. Make sure you save the new codes before closing the page.
For Developers
These examples describe UniAuth’s first-party, same-origin sign-in UI, not an integration API. Third-party apps must use hosted OAuth/OIDC Authorization Code with PKCE; they must not collect UniAuth passwords.
After data.requires2FA, complete password sign-in by posting email, password and twoFactorCode to /api/auth/login again. Use twoFactorMethod when selecting a method. No userId or pending-login token replaces the credentials; other required challenges, such as CAPTCHA, still apply.
// UniAuth's same-origin first-party UI only.
const loginRes = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const challenge = await loginRes.json();
if (!loginRes.ok || !challenge.success || !challenge.data?.requires2FA) {
handleLoginResult(challenge); // Includes completed login and other challenges.
} else {
// After prompting for a backup code, submit it with the credentials.
const verifyRes = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email, password, twoFactorCode: userInput, twoFactorMethod: 'backup'
})
});
const result = await verifyRes.json();
if (verifyRes.ok && result.success && result.data?.authenticated === true) {
// The server has established the session cookie.
redirectToAccount();
} else {
handleLoginResult(result);
}
}Apple and social sign-in use a separate pending credential: a browser cookie or a native pending credential (Apple returns applePendingToken; the continuation accepts it as pendingToken). Continue through /api/auth/social/2fa-options and /api/auth/social/2fa-verify; do not replace that flow with password login or /api/auth/2fa/verify.
Frequently Asked Questions
How many backup codes do I get?
You receive 8 backup codes when conditional enrollment issuance or an authorized regeneration succeeds.
Can I get more codes without invalidating my existing ones?
No. Regenerating codes always replaces the entire set. All previous codes are invalidated and 8 new codes are issued. This is a security measure to prevent accumulation of codes that may have been compromised.
What if I lose all my backup codes and my 2FA device?
If you lose every usable factor and backup proof, contact an authorized administrator for guarded account recovery from the Admin Panel → Userspage. The reset retires classic and push factors plus prior backup codes, preserves separate passkeys and the recovery phrase, and issues a one-time recovery credential; it does not simply restore password-only access.
Do backup codes expire?
A stored code has no scheduled expiry, but it stops being usable when claimed, replaced by regeneration, or retired by an account-recovery reset. Final admission and policy checks still apply.
Can I see which codes I have already used?
Your security settings show the remaining unused-code count. UniAuth retains only the keyed HMACs and count for remaining codes; a claimed code entry is atomically removed and is not displayed.