Overview
MediaStream supports Time-based One-Time Password (TOTP) two-factor authentication using authenticator apps like Google Authenticator, Authy, or 1Password. When enabled, users must provide both their password and a 6-digit code from their authenticator app to login.Two-factor authentication is configured per user and is completely optional.
2FA Endpoints
Enable 2FA
POST /user/two-factor-authenticationDisable 2FA
DELETE /user/two-factor-authenticationGet QR Code
GET /user/two-factor-qr-codeGet Secret Key
GET /user/two-factor-secret-keyGet Recovery Codes
GET /user/two-factor-recovery-codesRegenerate Codes
POST /user/two-factor-recovery-codesConfirm 2FA
POST /user/confirmed-two-factor-authentication2FA Challenge
POST /two-factor-challengeEnable Two-Factor Authentication
User must be authenticated to enable 2FA. Password confirmation may be required based on Fortify configuration.
Endpoint
Request Headers
CSRF token
Must be
application/jsonResponse
- 200 - Success
- 423 - Password Confirmation Required
Two-factor authentication enabled. Secret and recovery codes generated.
After enabling, retrieve the QR code and recovery codes for the user.
Example
Get QR Code
Retrieve a QR code for scanning with an authenticator app.Endpoint
Response
- 200 - Success
- 404 - Not Found
Returns an SVG QR code that users can scan with their authenticator app.
SVG markup for the QR code
Example
Get Secret Key
Retrieve the plain text secret key for manual entry into authenticator apps.Endpoint
Response
Base32-encoded secret key for manual entry
Example
Get Recovery Codes
Retrieve recovery codes that can be used if the user loses access to their authenticator device.Endpoint
Response
Example
Regenerate Recovery Codes
Generate a new set of recovery codes. This invalidates all previous codes.Endpoint
Response
After regenerating, immediately fetch the new codes with
GET /user/two-factor-recovery-codes.Example
Confirm Two-Factor Authentication
Confirm that 2FA is working correctly by verifying a code from the authenticator app.This step is required when
'confirm' => true is set in the Fortify configuration.Endpoint
Request Body
6-digit code from authenticator app
Response
- 200 - Success
- 422 - Invalid Code
Two-factor authentication confirmed and fully enabled.
Example
Two-Factor Challenge
Complete the two-factor authentication challenge during login.Endpoint
Request Body
Provide either a code from the authenticator app or a recovery code:6-digit code from authenticator app
Single-use recovery code (alternative to
code)Response
- 200 - Success
- 422 - Invalid Code
- 429 - Rate Limited
Authentication successful. User is now logged in.Session cookie is set and user can access protected routes.
Examples
Disable Two-Factor Authentication
Disable two-factor authentication for the authenticated user.Endpoint
Request Headers
CSRF token
Response
Example
Complete 2FA Setup Flow
Login Flow with 2FA
Database Fields
Two-factor authentication data is stored in theusers table:
All 2FA fields are encrypted at rest and never exposed in API responses.
Security Best Practices
Recovery Codes
Recovery Codes
- Instruct users to save recovery codes in a secure location
- Each recovery code can only be used once
- Regenerate codes if user suspects they’ve been compromised
Rate Limiting
Rate Limiting
- 2FA challenge endpoint is rate-limited to 5 attempts per minute
- This prevents brute-force attacks on 2FA codes
Time Synchronization
Time Synchronization
- TOTP codes are time-based (30-second window)
- Ensure server time is synchronized with NTP
- Codes have a small grace period for clock drift
Password Confirmation
Password Confirmation
- Require password confirmation before enabling/disabling 2FA
- Set
'confirmPassword' => truein Fortify configuration
Common Issues
Invalid authentication code
Invalid authentication code
- Ensure device time is synchronized correctly
- Code may have expired (codes change every 30 seconds)
- Verify the secret was scanned correctly
- Try using a recovery code instead
QR code not displaying
QR code not displaying
- User must enable 2FA first (
POST /user/two-factor-authentication) - Check that
Features::twoFactorAuthentication()is enabled - Ensure user is authenticated
Lost access to authenticator
Lost access to authenticator
- User can login using a recovery code
- After login, they can disable and re-enable 2FA
- If recovery codes are also lost, admin intervention is required
Related Endpoints
Login
Initial authentication
Profile Settings
Manage user settings