PHPackages                             pragmarx/google2fa-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. pragmarx/google2fa-laravel

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

pragmarx/google2fa-laravel
==========================

A One Time Password Authentication package, compatible with Google Authenticator.

v3.0.1(2mo ago)1.0k15.5M—1.5%195[57 issues](https://github.com/antonioribeiro/google2fa-laravel/issues)[3 PRs](https://github.com/antonioribeiro/google2fa-laravel/pulls)20MITPHPPHP &gt;=7.0CI passing

Since Jun 20Pushed 2mo ago23 watchersCompare

[ Source](https://github.com/antonioribeiro/google2fa-laravel)[ Packagist](https://packagist.org/packages/pragmarx/google2fa-laravel)[ RSS](/packages/pragmarx-google2fa-laravel/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (2)Dependencies (10)Versions (37)Used By (20)

Google2FA for Laravel
=====================

[](#google2fa-for-laravel)

 [![Latest Stable Version](https://camo.githubusercontent.com/3be4878262c8c87315d056d95bbbf48fbd7881245c2ddfe847bdcf01c298172a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f707261676d6172782f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pragmarx/google2fa-laravel) [![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE) [![Tests](https://camo.githubusercontent.com/0281b56076e32413f35759e36ec95889b415cc9a8b4dadcdc8ea2a19ad8e9cb8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e746f6e696f7269626569726f2f676f6f676c653266612d6c61726176656c2f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/antonioribeiro/google2fa-laravel/actions/workflows/run-tests.yml)

 [![Downloads](https://camo.githubusercontent.com/773eee7cb5dce0a1a11e4b7ca2c7761ab44bcf1a5ea705261eecfa5c59b4fc70/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f707261676d6172782f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pragmarx/google2fa-laravel) [![PHP](https://camo.githubusercontent.com/347272223e7a1e677c80f12e9d81dc7ee450d7616dbdf99f49b8a8a8d45b2a0a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302532302d2d253230382e352d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://php.net)

### Google Two-Factor Authentication Package 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](https://tools.ietf.org/html/rfc4226) and the Time-based One-time Password (TOTP) algorithm specified in [RFC 6238](https://tools.ietf.org/html/rfc6238).

This package is a Laravel bridge to [Google2FA](https://github.com/antonioribeiro/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

[](#recoverybackup-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](https://github.com/antonioribeiro/recovery).

Demos, Example &amp; Playground
-------------------------------

[](#demos-example--playground)

Please check the [Google2FA Package Playground](https://pragmarx.com/playground/google2fa#/).

[![playground](https://github.com/antonioribeiro/google2fa/raw/master/docs/playground.jpg)](https://github.com/antonioribeiro/google2fa/raw/master/docs/playground.jpg)

Here's an demo app showing how to use Google2FA: [google2fa-example](https://github.com/antonioribeiro/google2fa-example).

You can scan the QR code on [this (old) demo page](https://antoniocarlosribeiro.com/technology/google2fa) with a Google Authenticator app and view the code changing (almost) in real time.

Compatibility
-------------

[](#compatibility)

### Version 3.x

[](#version-3x)

LaravelPHP5.4.36+7.0 - 7.36.x7.2 - 8.07.x7.3 - 8.18.x7.3 - 8.29.x8.0 - 8.210.x8.1 - 8.311.x8.2 - 8.312.x8.2 - 8.413.x8.3 - 8.5### Version 2.x (Legacy)

[](#version-2x-legacy)

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

LaravelPHP5.2-5.85.6 - 7.36.x7.1 - 7.37.x7.2 - 7.4Install with: `composer require pragmarx/google2fa-laravel:^2.0`

### Historical Compatibility

[](#historical-compatibility)

Laravel[Google2FA](https://github.com/antonioribeiro/google2fa)Google2FA-Laravel4.2&lt;= 1.0.15.0-5.1&lt;= 1.0.15.2-10.x&gt;= 2.0.0&gt;= 0.2.0Before 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
----

[](#demo)

Click [here](https://pragmarx.com/playground/google2fa/middleware) to see the middleware demo:

[![middleware](docs/middleware.jpg)](docs/middleware.jpg)

Installing
----------

[](#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
---------------------

[](#installing-on-laravel)

### Laravel 5.5 and above

[](#laravel-55-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

[](#laravel-54-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):

```
PragmaRX\Google2FALaravel\ServiceProvider::class,

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

Publish the config file
-----------------------

[](#publish-the-config-file)

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

Using It
--------

[](#using-it)

#### Use the Facade

[](#use-the-facade)

```
use Google2FA;

return Google2FA::generateSecretKey();
```

#### In Laravel you can use the IoC Container

[](#in-laravel-you-can-use-the-ioc-container)

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

return $google2fa->generateSecretKey();
```

Middleware
----------

[](#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:

[](#add-the-middleware-to-your-kernelphp)

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

### Using it in one or more routes:

[](#using-it-in-one-or-more-routes)

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

### QRCode

[](#qrcode)

This package uses the [Google2FA-QRCode package](https://github.com/antonioribeiro/google2fa-qrcode), please check it for more info on how to configure the proper QRCode generators for your use case.

### Imagick QRCode Backend

[](#imagick-qrcode-backend)

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

You can change it via config:

```
/*
 * 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:

```
Google2FA::setQRCodeBackend('svg');
```

### Configuring the view

[](#configuring-the-view)

You can set your 'ask for a one time password' view in the config file (config/google2fa.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:

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

Here's a form example:

```

        Authenticate

```

One Time Password Lifetime
--------------------------

[](#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:

```
/**
* 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:

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

'keep_alive' => true,
```

Manually logging out from 2Fa
-----------------------------

[](#manually-logging-out-from-2fa)

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

```
Google2FA::logout();
```

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

```
use PragmaRX\Google2FALaravel\Support\Authenticator;

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

Throttling / Lockout after X attempts
-------------------------------------

[](#throttling--lockout-after-x-attempts)

Unless you need something really fancy, you can probably use Laravel's [route throttle middleware](https://laravel.com/docs/6.x/middleware) for that:

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

Stateless usage
---------------

[](#stateless-usage)

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

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

You can also use a stateless middleware:

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

2FA and Laravel login via remember
----------------------------------

[](#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`:

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

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

Events
------

[](#events)

The following events are fired:

- EmptyOneTimePasswordReceived
- LoggedOut
- LoginFailed
- LoginSucceeded
- OneTimePasswordExpired
- OneTimePasswordRequested

Documentation
-------------

[](#documentation)

Check the ReadMe file in the main [Google2FA](https://github.com/antonioribeiro/google2fa) repository.

Tests
-----

[](#tests)

The package tests were written with [phpspec](http://www.phpspec.net/en/latest/).

Author
------

[](#author)

[Antonio Carlos Ribeiro](http://twitter.com/iantonioribeiro)

License
-------

[](#license)

Google2FA is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

Contributing
------------

[](#contributing)

Pull requests and issues are more than welcome.

###  Health Score

72

—

ExcellentBetter than 100% of packages

Maintenance86

Actively maintained with recent releases

Popularity73

Solid adoption and visibility

Community48

Growing community involvement

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 74.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~94 days

Recently: every ~0 days

Total

35

Last Release

62d ago

Major Versions

v0.3.0 → v1.0.02019-03-21

v1.4.1 → v2.0.02021-08-15

v2.3.0 → v3.0.02026-03-17

PHP version history (2 changes)v0.1.0PHP &gt;=5.4

v1.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d9421a69d192db597169ec3519ad3efc6a35a660e4525e2ed08f6c753b908e86?d=identicon)[AntonioCarlosRibeiro](/maintainers/AntonioCarlosRibeiro)

---

Top Contributors

[![antonioribeiro](https://avatars.githubusercontent.com/u/3182864?v=4)](https://github.com/antonioribeiro "antonioribeiro (189 commits)")[![mfn](https://avatars.githubusercontent.com/u/87493?v=4)](https://github.com/mfn "mfn (15 commits)")[![asbiin](https://avatars.githubusercontent.com/u/25419741?v=4)](https://github.com/asbiin "asbiin (10 commits)")[![it-can](https://avatars.githubusercontent.com/u/644288?v=4)](https://github.com/it-can "it-can (4 commits)")[![robincsamuel](https://avatars.githubusercontent.com/u/4545992?v=4)](https://github.com/robincsamuel "robincsamuel (4 commits)")[![Khristophor](https://avatars.githubusercontent.com/u/6796588?v=4)](https://github.com/Khristophor "Khristophor (3 commits)")[![h44z](https://avatars.githubusercontent.com/u/1370804?v=4)](https://github.com/h44z "h44z (3 commits)")[![zeyad82](https://avatars.githubusercontent.com/u/16030521?v=4)](https://github.com/zeyad82 "zeyad82 (2 commits)")[![Luke-Shepp](https://avatars.githubusercontent.com/u/35956374?v=4)](https://github.com/Luke-Shepp "Luke-Shepp (2 commits)")[![beschoenen](https://avatars.githubusercontent.com/u/1887585?v=4)](https://github.com/beschoenen "beschoenen (2 commits)")[![davidrushton](https://avatars.githubusercontent.com/u/4039772?v=4)](https://github.com/davidrushton "davidrushton (2 commits)")[![emargareten](https://avatars.githubusercontent.com/u/46111162?v=4)](https://github.com/emargareten "emargareten (1 commits)")[![irayoun](https://avatars.githubusercontent.com/u/37749197?v=4)](https://github.com/irayoun "irayoun (1 commits)")[![CyrilMazur](https://avatars.githubusercontent.com/u/464216?v=4)](https://github.com/CyrilMazur "CyrilMazur (1 commits)")[![cyrildewit](https://avatars.githubusercontent.com/u/16477999?v=4)](https://github.com/cyrildewit "cyrildewit (1 commits)")[![Koozza](https://avatars.githubusercontent.com/u/1731647?v=4)](https://github.com/Koozza "Koozza (1 commits)")[![DeanWunder](https://avatars.githubusercontent.com/u/30644242?v=4)](https://github.com/DeanWunder "DeanWunder (1 commits)")[![cdn](https://avatars.githubusercontent.com/u/118273?v=4)](https://github.com/cdn "cdn (1 commits)")[![rbresjer](https://avatars.githubusercontent.com/u/11976380?v=4)](https://github.com/rbresjer "rbresjer (1 commits)")[![robertpcontreras-ts](https://avatars.githubusercontent.com/u/79461629?v=4)](https://github.com/robertpcontreras-ts "robertpcontreras-ts (1 commits)")

---

Tags

2faapp-securityauthenticationlaravelphptwo-factor-authenticationlaravelAuthenticationTwo Factor Authenticationgoogle2fa

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pragmarx-google2fa-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/pragmarx-google2fa-laravel/health.svg)](https://phpackages.com/packages/pragmarx-google2fa-laravel)
```

###  Alternatives

[pragmarx/google2fa-qrcode

QR Code package for Google2FA

12124.6M37](/packages/pragmarx-google2fa-qrcode)[ellaisys/aws-cognito

AWS Cognito package that allows Auth and other related features using the AWS SDK for PHP

120220.7k1](/packages/ellaisys-aws-cognito)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
