Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Google2Fa Laravel Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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).

Implementation Patterns

  • Secret Generation & QR Setup: In a user registration/2FA setup flow, generate and store the secret per user:
    $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.
  • Middleware Workflow: After successful password login, the 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.
  • Custom Views & Inputs: Override defaults via config:
    'view' => 'auth.google2fa', 'otp_input' => 'otp' — ensure your Blade template matches the config.
  • Stateless APIs: Use MiddlewareStateless or instantiate Authenticator::bootStateless($request) to validate OTP without sessions (e.g., mobile clients). Requires sending OTP on every request.
  • Backup Codes: Integrate the separate recovery package for generating and storing backup codes (not included here).
  • Security Enhancements: Set 'lifetime' => 15 (minutes) to re-prompt OTP periodically, and 'keep_alive' => false to disable session extension during browsing.

Gotchas and Tips

  • QR Backend Configuration: By default, uses ImageMagick (requires 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.
  • Laravel Remember Me Pitfall: When 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.
  • Session vs. Lifetime Conflict: If 'lifetime' > config('session.lifetime'), users get logged out prematurely. Always keep OTP lifetime ≤ session lifetime.
  • Middleware Failures: The 2fa middleware halts execution before the route closure if OTP is pending — avoid double validation. Ensure routes don’t manually check OTP after middleware.
  • Debugging OTP Validation: Enable 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).
  • Extensibility: Override auth logic by extending Authenticator or registering custom event listeners for LoginFailed, OneTimePasswordExpired, etc., to log, lock, or alert on anomalies.
  • Deprecated Laravel Versions: For Laravel ≤5.1, install pragmarx/google2fa:~1.0 (the old monolithic package), not this bridge. This package (google2fa-laravel) requires ≥2.0 of the core google2fa library.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport