Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

google2fa-laravel

> 后端框架
Open source

A One Time Password Authentication package, compatible with Google Authenticator for Laravel

1.1K stars0 likes3 views
WebsiteGitHub

About

A One Time Password Authentication package, compatible with Google Authenticator for Laravel

Google2FA for Laravel

Google Two-Factor Authentication Package for Laravel

Google2FA is a PHP implementation of the Google Two-Factor Authentication Module, supporting the HMAC-Based One-time Password (HOTP) algorithm specified in RFC 4226 and the Time-based One-time Password (TOTP) algorithm specified in RFC 6238.

This package is a Laravel bridge to Google2FA's PHP package.

The intent of this package is to create QRCodes for Google2FA and check user typed codes. If you need to create backup/recovery codes, please check below.

Recovery/Backup codes

if you need to create recovery or backup codes to provide a way for your users to recover a lost account, you can use the Recovery Package.

Demos, Example & Playground

Please check the Google2FA Package Playground.

Here's an demo app showing how to use Google2FA: google2fa-example.

You can scan the QR code on this (old) demo page with a Google Authenticator app and view the code changing (almost) in real time.

Compatibility

Version 3.x

Laravel PHP
5.4.36+ 7.0 - 7.3
6.x 7.2 - 8.0
7.x 7.3 - 8.1
8.x 7.3 - 8.2
9.x 8.0 - 8.2
10.x 8.1 - 8.3
11.x 8.2 - 8.3
12.x 8.2 - 8.4
13.x 8.3 - 8.5

Version 2.x (Legacy)

If you need PHP 5 support, use version 2.x:

Laravel PHP
5.2-5.8 5.6 - 7.3
6.x 7.1 - 7.3
7.x 7.2 - 7.4

Install with: composer require pragmarx/google2fa-laravel:^2.0

Historical Compatibility

Laravel Google2FA Google2FA-Laravel
4.2 = 2.0.0 >= 0.2.0

Before Google2FA 2.0 (Laravel 5.1) you have to install pragmarx/google2fa:~1.0, because this package was both a Laravel package and a PHP (agnostic).

Demo

Click here to see the middleware demo:

Installing

Use Composer to install it:

Latest version (3.x - requires PHP 7.0+):

composer require pragmarx/google2fa-laravel

Legacy version (2.x - supports PHP 5):

composer require pragmarx/google2fa-laravel:^2.0

Installing on Laravel

Laravel 5.5 and above

You don't have to do anything else, this package autoloads the Service Provider and create the Alias, using the new Auto-Discovery feature.

Laravel 5.4 and below

Add the Service Provider and Facade alias to your app/config/app.php (Laravel 4.x) or config/app.php (Laravel 5.x):

php
PragmaRX\Google2FALaravel\ServiceProvider::class,

'Google2FA' => PragmaRX\Google2FALaravel\Facade::class,

Publish the config file

php
php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"

Using It

Use the Facade

php
use Google2FA;

return Google2FA::generateSecretKey();

In Laravel you can use the IoC Container

php
$google2fa = app('pragmarx.google2fa');

return $google2fa->generateSecretKey();

Middleware

This package has a middleware which will help you code 2FA on your app. To use it, you just have to:

Add the middleware to your Kernel.php:

php
protected $routeMiddleware = [
    ...
    '2fa' => \PragmaRX\Google2FALaravel\Middleware::class,
];

Using it in one or more routes:

php
Route::get('/admin', function () {
    return view('admin.index');
})->middleware(['auth', '2fa']);

QRCode

This package uses the Google2FA-QRCode package, please check it for more info on how to configure the proper QRCode generators for your use case.

Imagick QRCode Backend

There are three available: imagemagick (default), svg and eps.

You can change it via config:

php
/*
 * Which image backend to use for generating QR codes?
 *
 * Supports imagemagick, svg and eps
 */
'qrcode_image_backend' => \PragmaRX\Google2FALaravel\Support\Constants::QRCODE_IMAGE_BACKEND_IMAGEMAGICK,

Or runtime:

php
Google2FA::setQRCodeBackend('svg');

Configuring the view

You can set your 'ask for a one time password' view in the config file (config/google2fa.php):

php
/**
 * One Time Password View
 */
'view' => 'google2fa.index',

And in the view you just have to provide a form containing the input, which is also configurable:

php
/**
 * One Time Password request input name
 */
'otp_input' => 'one_time_password',

Here's a form example:

xml
    
        

        Authenticate
    

One Time Password Lifetime

Usually an OTP lasts forever, until the user logs off your app, but, to improve application safety, you may want to re-ask, only for the Google OTP, from time to time. So you can set a number of minutes here:

php
/**
* Lifetime in minutes.
* In case you need your users to be asked for a new one time passwords from time to time.
*/

'lifetime' => 0, // 0 = eternal

Keep in mind that this uses the Laravel sessions in the background. If this number exceeds the value set in config('session.lifetime') you will still be logged out, even if your OTP lifetime has not expired.

And you can decide whether your OTP will be kept alive while your users are browsing the site or not:

php
/**
 * Renew lifetime at every new request.
 */

'keep_alive' => true,

Manually logging out from 2Fa

This command will logout your user and redirect he/she to the 2FA form on the next request:

php
Google2FA::logout();

If you don't want to use the Facade, you may:

php
use PragmaRX\Google2FALaravel\Support\Authenticator;

(new Authenticator(request()))->logout();

Throttling / Lockout after X attempts

Unless you need something really fancy, you can probably use Laravel's route throttle middleware for that:

php
Route::get('/admin', function () {
    return view('admin.index');
})->middleware(['auth', '2fa', 'throttle']);

Stateless usage

php
$authenticator = app(Authenticator::class)->bootStateless($request);

if ($authenticator->isAuthenticated()) {
    // otp auth success!
}

You can also use a stateless middleware:

php
protected $routeMiddleware = [
    ...
    '2fa' => \PragmaRX\Google2FALaravel\MiddlewareStateless::class,
];

2FA and Laravel login via remember

When Laravel login via remember is activated, the session is renovated and the 2FA code is required again. To solve this, add the LoginViaRemember listener in your App\Providers\EventServiceProvider:

php
use Illuminate\Auth\Events\Login;
use PragmaRX\Google2FALaravel\Listeners\LoginViaRemember;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Login::class => [
            LoginViaRemember::class,
        ],
    ];
...

Events

The following events are fired:

  • EmptyOneTimePasswordReceived
  • LoggedOut
  • LoginFailed
  • LoginSucceeded
  • OneTimePasswordExpired
  • OneTimePasswordRequested

Documentation

Check the ReadMe file in the main Google2FA repository.

Tests

The package tests were written with phpspec.

Author

Antonio Carlos Ribeiro

License

Google2FA is licensed under the MIT License - see the LICENSE file for details

Contributing

Pull requests and issues are more than welcome.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP2faapp-securityauthenticationlaravel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架