PHPackages                             roke22/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. roke22/google2fa-laravel

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

roke22/google2fa-laravel
========================

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

2.2(2y ago)05MITPHPPHP &gt;=7.0

Since Sep 30Pushed 2y agoCompare

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

READMEChangelog (1)Dependencies (5)Versions (3)Used By (0)

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) [![Code Quality](https://camo.githubusercontent.com/5d3950dc76c9c1973ebdd1cf1d65c687b267ca2990c8ca304c2de4b31b2e69cd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616e746f6e696f7269626569726f2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/antonioribeiro/google2fa-laravel/?branch=master) [![Build](https://camo.githubusercontent.com/f066ea22eae98f26135ae18c3e7c926b2c80c6899ca4258f296791787ceb3db9/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f616e746f6e696f7269626569726f2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/antonioribeiro/google2fa-laravel)

 [![Downloads](https://camo.githubusercontent.com/773eee7cb5dce0a1a11e4b7ca2c7761ab44bcf1a5ea705261eecfa5c59b4fc70/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f707261676d6172782f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pragmarx/google2fa-laravel) [![Coverage](https://camo.githubusercontent.com/046e569ea2513db62a00e0b78f8f1fa1bc9fcf24d49aa07b18333693a640f226/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f616e746f6e696f7269626569726f2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/antonioribeiro/google2fa-laravel/?branch=master) [![StyleCI](https://camo.githubusercontent.com/fb7273fff4a133a673f752405dcbf92a9ea67bec5679e467fb7e477eb47141c0/68747470733a2f2f7374796c6563692e696f2f7265706f732f39343633303835312f736869656c64)](https://styleci.io/repos/94630851) [![PHP](https://camo.githubusercontent.com/4f2892097b766a52133d4c68797e7ba80c40f3e7907d2882236a1cc0518f0746/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302532302d2d253230382e302d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/antonioribeiro/google2fa-laravel)

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

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:

```
composer require pragmarx/google2fa-laravel

```

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,
];
```

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

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 76.3% 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

Unknown

Total

1

Last Release

954d ago

### Community

Maintainers

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

---

Top Contributors

[![antonioribeiro](https://avatars.githubusercontent.com/u/3182864?v=4)](https://github.com/antonioribeiro "antonioribeiro (184 commits)")[![asbiin](https://avatars.githubusercontent.com/u/25419741?v=4)](https://github.com/asbiin "asbiin (10 commits)")[![mfn](https://avatars.githubusercontent.com/u/87493?v=4)](https://github.com/mfn "mfn (6 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)")[![h44z](https://avatars.githubusercontent.com/u/1370804?v=4)](https://github.com/h44z "h44z (3 commits)")[![Khristophor](https://avatars.githubusercontent.com/u/6796588?v=4)](https://github.com/Khristophor "Khristophor (3 commits)")[![roke22](https://avatars.githubusercontent.com/u/5707837?v=4)](https://github.com/roke22 "roke22 (2 commits)")[![davidrushton](https://avatars.githubusercontent.com/u/4039772?v=4)](https://github.com/davidrushton "davidrushton (2 commits)")[![zeyad82](https://avatars.githubusercontent.com/u/16030521?v=4)](https://github.com/zeyad82 "zeyad82 (2 commits)")[![beschoenen](https://avatars.githubusercontent.com/u/1887585?v=4)](https://github.com/beschoenen "beschoenen (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)")[![benrolfe](https://avatars.githubusercontent.com/u/474175?v=4)](https://github.com/benrolfe "benrolfe (1 commits)")[![cdn](https://avatars.githubusercontent.com/u/118273?v=4)](https://github.com/cdn "cdn (1 commits)")[![m1r0](https://avatars.githubusercontent.com/u/1612178?v=4)](https://github.com/m1r0 "m1r0 (1 commits)")[![arukompas](https://avatars.githubusercontent.com/u/8697942?v=4)](https://github.com/arukompas "arukompas (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)")[![roelofjan-elsinga](https://avatars.githubusercontent.com/u/9220754?v=4)](https://github.com/roelofjan-elsinga "roelofjan-elsinga (1 commits)")

---

Tags

laravelAuthenticationTwo Factor Authenticationgoogle2fa

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[pragmarx/google2fa-laravel

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

1.0k15.5M63](/packages/pragmarx-google2fa-laravel)[pragmarx/google2fa-qrcode

QR Code package for Google2FA

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