Overview
SaaS Starter Vue includes built-in two-factor authentication (2FA) using Laravel Fortify’s implementation. This provides an additional layer of security by requiring users to verify their identity with a time-based one-time password (TOTP) in addition to their password.2FA uses authenticator apps like Google Authenticator, Authy, or 1Password to generate verification codes.
How It Works
2FA requires users to:- Enter their email and password (first factor)
- Enter a 6-digit code from their authenticator app (second factor)
- Optionally use recovery codes if they lose access to their authenticator
TOTP (Time-Based One-Time Password)
The system uses the TOTP algorithm (RFC 6238) which generates codes that expire every 30 seconds. This is the same standard used by Google Authenticator and other popular authenticator apps.Configuration
Enable 2FA Feature
2FA is enabled in the Fortify configuration:Require users to confirm 2FA setup with a valid code before activation
Require password confirmation before enabling/disabling 2FA
Database Schema
The users table includes 2FA columns:Encrypted TOTP secret key used to generate verification codes
Encrypted JSON array of one-time recovery codes
Timestamp when 2FA was successfully confirmed and activated
User Model Setup
The User model includes theTwoFactorAuthenticatable trait:
Enabling 2FA
Step-by-Step Setup
1
Navigate to Security Settings
Users access their security settings page (typically
/settings/security)2
Confirm Password
User enters their current password to authorize 2FA changes
3
Enable Two-Factor
User clicks “Enable Two-Factor Authentication”
4
Scan QR Code
System generates and displays a QR code. User scans it with their authenticator app
5
Verify Setup
User enters a code from their authenticator app to confirm setup
6
Save Recovery Codes
System generates 8 recovery codes. User must save them securely
API Endpoints
Enable 2FA
- Generates a new TOTP secret
- Creates recovery codes
- Stores encrypted secret in database
Get QR Code
Get Secret Key
Get Recovery Codes
Regenerate Recovery Codes
Disabling 2FA
Users can disable 2FA:1
Navigate to Security Settings
Access the security settings page
2
Confirm Password
Enter current password to authorize the change
3
Disable 2FA
Click “Disable Two-Factor Authentication”
4
Confirmation
2FA is immediately disabled and secret/codes are removed from database
Login with 2FA
Two-Factor Challenge Flow
When 2FA is enabled, the login process changes:1
Enter Credentials
User enters email and password at
/auth/login2
Redirect to 2FA Challenge
Instead of logging in, user is redirected to
/auth/two-factor-challenge3
Enter Verification Code
User enters the 6-digit code from their authenticator app (or a recovery code)
4
Submit Challenge
5
Login Successful
User is authenticated and redirected to dashboard
Challenge Endpoint
Rate Limiting
2FA challenges are rate-limited separately:Recovery Codes
What Are Recovery Codes?
Recovery codes are one-time use backup codes that allow users to access their account if they:- Lose their authenticator device
- Can’t access their authenticator app
- Need emergency access
Recovery Code Format
By default, Laravel Fortify generates 8 recovery codes that look like:Using Recovery Codes
Users can enter a recovery code instead of a TOTP code during login:- Click “Use recovery code” on the 2FA challenge page
- Enter one of their saved recovery codes
- Code is validated and invalidated (can only be used once)
- User is logged in
Once a recovery code is used, it’s permanently invalidated. Users should regenerate recovery codes after using them.
Regenerating Recovery Codes
Users should regenerate codes if:- They’ve used some recovery codes
- They suspect codes were compromised
- They want to invalidate old codes
Security Considerations
Password Confirmation
Sensitive 2FA operations require password confirmation:Best Practices
Enforcing 2FA
You can require 2FA for specific users or roles:Checking 2FA Status
In your blade/Vue templates:Testing 2FA
Test 2FA in your application:Troubleshooting
Codes not working
Codes not working
Common causes:
- Time sync issues - Ensure device clock is accurate (TOTP is time-based)
- Wrong secret - User may have scanned QR code for different account
- App issues - Try different authenticator app
Lost authenticator device
Lost authenticator device
Options:
- Use a recovery code to log in
- Contact support to disable 2FA (requires identity verification)
- Use backup authenticator if codes were synced (like Authy)
QR code not displaying
QR code not displaying
Check:
- User is authenticated
- 2FA is enabled for the user (
two_factor_secretexists) - Route middleware is correct
- SVG rendering is supported in browser
Customization
You can customize the 2FA experience:Custom 2FA Views
Create custom Inertia/Vue pages:Custom Recovery Code Count
Modify the recovery code generation in a service provider:Backup Methods
Consider implementing backup authentication methods:- SMS verification (requires additional package)
- Email verification codes
- Hardware security keys (WebAuthn)
Related Documentation
- Authentication System - General authentication overview
- User Management - Managing users and permissions
- Security Best Practices - Overall security guidelines