Two-Factor Authentication
Two-Factor Authentication (2FA) adds an extra layer of security to your authentication system by requiring users to provide two different authentication factors to verify their identity.
Overview
Svelte Guardian supports Time-Based One-Time Password (TOTP) two-factor authentication, commonly used with authenticator apps like Google Authenticator, Authy, and Microsoft Authenticator.
Configuration
Configure 2FA in your Svelte Guardian setup:
Implementation
Enabling 2FA for Users
2FA setup typically involves these steps:
- Generate a secret for the user
- Display the QR code to the user
- Verify the user can generate valid codes
- Save the verified secret to the user’s account
- Generate backup codes (optional)
Server-Side Implementation
Svelte Guardian provides endpoints for handling 2FA setup and verification:
POST:/auth/2fa/generate
: Generates a new TOTP secretPOST:/auth/2fa/verify
: Verifies a TOTP code and enables 2FAPOST:/auth/2fa/disable
: Disables 2FA for a userPOST:/auth/2fa/backup-codes
: Generates backup codes
Here’s a server-side example showing how to handle these requests:
Client-Side Implementation
Create a 2FA setup flow in your application:
Set Up Two-Factor Authentication
Two-factor authentication adds an extra layer of security to your account.
{:else if step === 'scan'}Scan QR Code
Scan this QR code with your authenticator app:
If you can't scan the QR code, enter this code manually:
Now enter the verification code from your authenticator app:
{#if error}Backup Codes
Important: Save these backup codes in a secure place. They can be used to regain access to your account if you lose your authenticator device.
Login with 2FA
When 2FA is enabled for a user, they’ll need to complete a two-step login process:
- Enter their credentials (email/password or OAuth)
- Enter the time-based code from their authenticator app
Handling 2FA During Login
Svelte Guardian automatically handles the 2FA challenge during login. When a user with 2FA enabled attempts to log in, the signIn
function will return a result indicating that a 2FA code is required:
2FA Verification Page
Create a page for 2FA code verification:
Two-Factor Authentication
Enter the verification code from your authenticator app.
{#if error}Using Backup Codes
If a user loses access to their authenticator device, they can use one of their backup codes to log in:
Use Backup Code
Enter one of your backup codes to sign in.
{#if error}Note: Each backup code can only be used once. After using a backup code, you should disable 2FA and set it up again if you still don't have access to your authenticator app.
Disabling 2FA
Allow users to disable 2FA if needed:
Security Considerations
Account Recovery: Always provide backup codes or an alternative recovery method.
Password Confirmation: Require password confirmation when enabling or disabling 2FA.
Session Management: Invalidate other sessions when 2FA is enabled or disabled.
Remember Devices (optional): Consider allowing users to trust devices to reduce 2FA prompts.
Rate Limiting: Apply strict rate limits to 2FA verification endpoints.
Best Practices
Clear Instructions: Provide clear guidance for users setting up 2FA.
QR Code and Manual Entry: Support both QR code scanning and manual secret entry.
Testing: Verify code generation before finalizing setup.
Backup Codes: Generate and require users to save backup codes.
Application Name: Set a clear issuer name for the authenticator app.