metasoftdevs / laravel-breeze-2fa
Advanced two-factor authentication package for Laravel Breeze with multi-channel support (TOTP, Email, SMS) and custom authentication integration
Requires
- php: ^8.1
- bacon/bacon-qr-code: ^2.0
- illuminate/contracts: ^10.0|^11.0|^12.0
- laravel/framework: ^10.0|^11.0|^12.0
- pragmarx/google2fa: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- larastan/larastan: ^2.0
- laravel/breeze: ^1.0|^2.0
- mockery/mockery: ^1.4
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0
Suggests
- laravel/breeze: For seamless Breeze integration
- laravel/horizon: For queue monitoring when using email/SMS OTP
- twilio/sdk: Required for SMS OTP via Twilio (^7.0)
- vonage/client: Required for SMS OTP via Vonage/Nexmo (^4.0)
This package is not auto-updated.
Last update: 2025-07-27 17:31:48 UTC
README
An advanced, highly customizable two-factor authentication (2FA) package for Laravel Breeze that supports multiple authentication methods and seamlessly integrates with both Breeze and custom authentication systems.
โจ Features
- ๐ Multiple 2FA Methods: TOTP (Authenticator Apps), Email OTP, SMS OTP
- ๐ Recovery Codes: Secure backup codes for account recovery
- ๐ฑ Device Remembering: Optional "trust this device" functionality
- ๐ก๏ธ Security First: Rate limiting, encryption, CSRF protection
- ๐จ Laravel Breeze Integration: Drop-in compatibility with Breeze
- ๐ง Highly Customizable: Extensive configuration options
- ๐ Custom Auth Support: Works with any Laravel authentication guard
- ๐งช Fully Tested: Comprehensive test suite with 90%+ coverage
- ๐ Well Documented: Extensive documentation and examples
๐ Requirements
- PHP 8.1 or higher
- Laravel 10.0 or higher
- Laravel Breeze (for seamless integration)
๐ Quick Start
Installation
Install the package via Composer:
composer require metasoftdevs/laravel-breeze-2fa
Basic Setup
- Publish and run migrations:
php artisan vendor:publish --provider="MetaSoftDevs\LaravelBreeze2FA\TwoFactorServiceProvider" --tag="two-factor-migrations" php artisan migrate
- Publish configuration (optional):
php artisan vendor:publish --provider="MetaSoftDevs\LaravelBreeze2FA\TwoFactorServiceProvider" --tag="two-factor-config"
- Install the package setup:
php artisan two-factor:install
Laravel Breeze Integration
If you're using Laravel Breeze, the package will automatically integrate with your existing authentication flow:
// In your login controller or middleware use MetaSoftDevs\LaravelBreeze2FA\Facades\TwoFactor; // After successful password authentication if (TwoFactor::isEnabledForUser($user)) { if (!TwoFactor::isDeviceRemembered($user)) { // Redirect to 2FA challenge return redirect()->route('two-factor.challenge'); } }
๐ Usage Examples
Enabling 2FA for a User
use MetaSoftDevs\LaravelBreeze2FA\Facades\TwoFactor; // Enable TOTP (Authenticator App) $setup = TwoFactor::enable($user, 'totp'); $qrCodeUrl = $setup['qr_code_url']; $secret = $setup['secret']; $recoveryCodes = $setup['recovery_codes']; // Enable Email OTP $setup = TwoFactor::enable($user, 'email'); // A verification email will be sent automatically // Enable SMS OTP $setup = TwoFactor::enable($user, 'sms'); // A verification SMS will be sent automatically
Confirming 2FA Setup
// User enters the code from their authenticator app/email/SMS $isConfirmed = TwoFactor::confirm($user, $userProvidedCode); if ($isConfirmed) { // 2FA is now active for the user return redirect()->route('dashboard')->with('success', '2FA enabled successfully!'); }
Verifying 2FA During Login
// In your authentication flow try { $verified = TwoFactor::verify($user, $code, $rememberDevice = true); if ($verified) { // User is authenticated, proceed with login Auth::login($user); return redirect()->intended('dashboard'); } } catch (\MetaSoftDevs\LaravelBreeze2FA\Exceptions\InvalidCodeException $e) { return back()->withErrors(['code' => 'Invalid verification code']); } catch (\MetaSoftDevs\LaravelBreeze2FA\Exceptions\RateLimitExceededException $e) { return back()->withErrors(['code' => 'Too many attempts. Please try again later.']); }
Disabling 2FA
$disabled = TwoFactor::disable($user); if ($disabled) { return redirect()->back()->with('success', '2FA has been disabled.'); }
Getting User's 2FA Status
$status = TwoFactor::getStatus($user); /* Returns: [ 'enabled' => true, 'method' => 'totp', 'confirmed' => true, 'recovery_codes_count' => 6, 'can_generate_recovery_codes' => true ] */
๐ง Configuration
The package offers extensive configuration options. Publish the config file to customize:
php artisan vendor:publish --provider="MetaSoftDevs\LaravelBreeze2FA\TwoFactorServiceProvider" --tag="two-factor-config"
Key Configuration Options
// config/two-factor.php return [ // Enable/disable the entire 2FA system 'enabled' => env('TWO_FACTOR_ENABLED', true), // Require all users to set up 2FA 'required' => env('TWO_FACTOR_REQUIRED', false), // Configure available methods 'methods' => [ 'totp' => [ 'enabled' => true, 'issuer' => env('APP_NAME'), 'window' => 1, // Time drift tolerance ], 'email' => [ 'enabled' => true, 'expiry' => 300, // 5 minutes ], 'sms' => [ 'enabled' => false, 'provider' => 'twilio', ], ], // Recovery codes settings 'recovery_codes' => [ 'enabled' => true, 'count' => 8, 'length' => 10, ], // Device remembering 'remember_device' => [ 'enabled' => true, 'duration' => 30 * 24 * 60, // 30 days ], // Rate limiting 'rate_limiting' => [ 'max_attempts' => 5, 'decay_minutes' => 15, ], ];
๐ SMS Configuration
For SMS OTP, configure your provider credentials:
Twilio
TWILIO_ACCOUNT_SID=your_account_sid TWILIO_AUTH_TOKEN=your_auth_token TWILIO_PHONE_NUMBER=your_twilio_number
Vonage (Nexmo)
VONAGE_API_KEY=your_api_key VONAGE_API_SECRET=your_api_secret VONAGE_PHONE_NUMBER=your_sender_id
๐จ Frontend Integration
Blade Templates
The package includes pre-built Blade templates that you can customize:
php artisan vendor:publish --provider="MetaSoftDevs\LaravelBreeze2FA\TwoFactorServiceProvider" --tag="two-factor-views"
Vue.js/Inertia.js
For Vue.js applications using Inertia:
// Setup 2FA const setup2FA = async (method) => { const response = await axios.post("/two-factor/enable", { method }); if (method === "totp") { // Show QR code: response.data.qr_code_url showQRCode(response.data.qr_code_url); } // Show recovery codes showRecoveryCodes(response.data.recovery_codes); }; // Verify setup const confirm2FA = async (code) => { await axios.post("/two-factor/confirm", { code }); // 2FA is now enabled };
Livewire
For Livewire components:
class TwoFactorSetup extends Component { public $method = 'totp'; public $code = ''; public $qrCodeUrl = ''; public $recoveryCodes = []; public function enable() { $setup = TwoFactor::enable(auth()->user(), $this->method); $this->qrCodeUrl = $setup['qr_code_url'] ?? ''; $this->recoveryCodes = $setup['recovery_codes'] ?? []; } public function confirm() { TwoFactor::confirm(auth()->user(), $this->code); session()->flash('message', '2FA enabled successfully!'); } }
๐ ๏ธ Custom Authentication Integration
For custom authentication systems, implement the 2FA flow manually:
use MetaSoftDevs\LaravelBreeze2FA\Facades\TwoFactor; // In your custom login controller class CustomLoginController extends Controller { public function authenticate(Request $request) { // Your existing authentication logic $user = $this->attemptLogin($request); if ($user && TwoFactor::isEnabledForUser($user)) { if (!TwoFactor::isDeviceRemembered($user)) { // Store user in session for 2FA challenge session(['2fa_user_id' => $user->id]); return redirect()->route('two-factor.challenge'); } } // Complete login Auth::login($user); return redirect()->intended(); } public function challenge() { // Show 2FA challenge form return view('auth.two-factor-challenge'); } public function verify(Request $request) { $userId = session('2fa_user_id'); $user = User::find($userId); $verified = TwoFactor::verify($user, $request->code, $request->boolean('remember')); if ($verified) { session()->forget('2fa_user_id'); Auth::login($user); return redirect()->intended(); } return back()->withErrors(['code' => 'Invalid code']); } }
๐งช Testing
Run the package tests:
composer test
Run tests with coverage:
composer test-coverage
๐ Commands
The package includes several Artisan commands:
# Install the package (publish assets, run migrations) php artisan two-factor:install # Generate recovery codes for a user php artisan two-factor:recovery-codes {user-id} # Clean up expired sessions and codes php artisan two-factor:cleanup # Show 2FA statistics php artisan two-factor:stats
๐ Security Considerations
- Secrets Encryption: TOTP secrets are encrypted in the database
- Rate Limiting: Prevents brute force attacks on 2FA codes
- Recovery Codes: Securely hashed and single-use
- Device Tokens: Cryptographically secure device remembering
- Audit Trail: All authentication attempts are logged
- CSRF Protection: All forms include CSRF tokens
๐ค Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
Development Setup
- Clone the repository
- Install dependencies:
composer install
- Run tests:
composer test
- Check code style:
composer format
๐ Changelog
Please see CHANGELOG.md for more information on what has changed recently.
๐ก๏ธ Security
If you discover any security-related issues, please email security@metasoft.dev instead of using the issue tracker.
๐ License
The MIT License (MIT). Please see License File for more information.
๐ Credits
๐ Related Packages
- Laravel Breeze - Simple authentication scaffolding
- Laravel Fortify - Backend authentication services
- pragmarx/google2fa - Google2FA for Laravel
Built with โค๏ธ by Meta Software Developers
info@metasoftdevs.com