pragmarx/google2fa-laravel
Laravel bridge for pragmarx/google2fa: generate QR codes and verify HOTP/TOTP (RFC 4226/6238) two-factor authentication codes compatible with Google Authenticator and similar apps. Includes config, middleware-friendly integration, and optional recovery codes via separate package.
Begin by installing the package via Composer:
composer require pragmarx/google2fa-laravel
For Laravel 5.5+, service provider and facade auto-discovery work out-of-the-box; older versions require manual registration in config/app.php. Publish the config file to customize behavior:
php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"
The first use case is enabling 2FA middleware on authenticated routes (e.g., /admin). Register the middleware in app/Http/Kernel.php, then apply it alongside auth:
Route::get('/admin', fn() => '...')->middleware(['auth', '2fa']);
Configure the ask for a one time password view path in config/google2fa.php and create the Blade template with a form (default input name: one_time_password).
$secret = Google2FA::generateSecretKey();
$user->google2fa_secret = $secret;
$user->save();
$qrUrl = Google2FA::getQRCodeUrl('Your App', $secret);
Render $qrUrl as an <img src="..."> or use Google2FA::getQRCodeInline() for inline SVG/EMF.2fa middleware intercepts requests, checks session-stored OTP state, and redirects to the OTP prompt view. On successful OTP validation, the user proceeds; otherwise, fails authentication.'view' => 'auth.google2fa', 'otp_input' => 'otp' — ensure your Blade template matches the config.MiddlewareStateless or instantiate Authenticator::bootStateless($request) to validate OTP without sessions (e.g., mobile clients). Requires sending OTP on every request.'lifetime' => 15 (minutes) to re-prompt OTP periodically, and 'keep_alive' => false to disable session extension during browsing.ext-imagick). If unavailable, switch to svg or eps in config/google2fa.php or call Google2FA::setQRCodeBackend('svg') at runtime. Without BaconQrCode installed, QR generation will silently fail — ensure required packages (bacon/bacon-qr-code + backend driver) are present.remember_me is used, sessions are renewed but 2FA still triggers on next request. Fix by adding LoginViaRemember listener in EventServiceProvider to clear 2FA state on remember-me login.'lifetime' > config('session.lifetime'), users get logged out prematurely. Always keep OTP lifetime ≤ session lifetime.2fa middleware halts execution before the route closure if OTP is pending — avoid double validation. Ensure routes don’t manually check OTP after middleware.debug mode (in config or Google2FA::setDebug(true)) to log failed attempts with human-readable messages. Check for common timing issues (e.g., server clock skew) — the underlying TOTP algorithm tolerates ±1 time step (±30s).Authenticator or registering custom event listeners for LoginFailed, OneTimePasswordExpired, etc., to log, lock, or alert on anomalies.pragmarx/google2fa:~1.0 (the old monolithic package), not this bridge. This package (google2fa-laravel) requires ≥2.0 of the core google2fa library.How can I help you explore Laravel packages today?