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

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

diadal/google2fa-laravel
========================

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

v0.2.0(8y ago)110BSD-3-ClausePHPPHP &gt;=5.4

Since Jun 20Pushed 7y ago1 watchersCompare

[ Source](https://github.com/diadal/google2fa-laravel)[ Packagist](https://packagist.org/packages/diadal/google2fa-laravel)[ RSS](/packages/diadal-google2fa-laravel/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (7)Used By (0)

Google2FA for Laravel Using Web &amp; Admin Auth Guard
======================================================

[](#google2fa-for-laravel-using-web--admin-auth-guard)

 [![Latest Stable Version](https://camo.githubusercontent.com/bc09a11e8243ee110313c898e29c3111877a75d419e6c14c34989c51ae5f192e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64696164616c2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/diadal/google2fa-laravel) [![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE) [![Code Quality](https://camo.githubusercontent.com/604e32acadac1c6f14b93d92416657dab41f132e812a9f728e3c8d4120f3f499/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f64696164616c2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/diadal/google2fa-laravel/?branch=master) [![Build](https://camo.githubusercontent.com/61098e822b5a751532ec310a3218a0ed78a413c8b559b9c0d8b502cb5dce8be5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f64696164616c2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/diadal/google2fa-laravel) [![Downloads](https://camo.githubusercontent.com/3250bf0f257f05f96520bc168915caeb740a5b87ba6b19e4cdac26901ed0eca8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64696164616c2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/diadal/google2fa-laravel)

 [![Coverage](https://camo.githubusercontent.com/c5665180ba43d1d6673a3dad2c6b1049416fe02af01f017280f94f50264f039d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f64696164616c2f676f6f676c653266612d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/diadal/google2fa-laravel/?branch=master) [![Build Status](https://camo.githubusercontent.com/64dda8063ad188edac7a2a8c1edfabe1f8a144286e511e3e2fde1547b11c0cf6/68747470733a2f2f7472617669732d63692e6f72672f64696164616c2f70617373706f72742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/diadal/google2fa-laravel) [![PHP](https://camo.githubusercontent.com/65f70ca194dd25d0c82a973168c48e0d2d13b0162ef9bb7ea4b303f39aa9af96/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e302532302d2d253230372e322d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/diadal/google2fa-laravel)

### Google Two-Factor Authentication Package for Laravel Useing web &amp; admin Auth Guard

[](#google-two-factor-authentication-package-for-laravel-useing-web--admin-auth-guard)

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://diadal.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-5.6&gt;= 2.0.0&gt;= 0.2.0Installing
----------

[](#installing)

Use Composer to install it:

```
composer require diadal/google2fa-laravel

```

If you prefer inline QRCodes instead of a Google generated url, you'll need to install [BaconQrCode](https://github.com/Bacon/BaconQrCode):

```
composer require bacon/bacon-qr-code

```

Installing on Laravel
---------------------

[](#installing-on-laravel)

### Laravel 5.5

[](#laravel-55)

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

```
Diadal\Google2FALaravel\ServiceProvider::class,

'Diadal2FA' => Diadal\Google2FALaravel\Facade::class,
```

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

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

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

Using It
--------

[](#using-it)

#### Use the Facade

[](#use-the-facade)

```
use Diadal2FA;

return Diadal2FA::generateSecretKey();
```

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

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

```
$google2fa = app('diadal.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:

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

Using the middleware
--------------------

[](#using-the-middleware)

### Add the middleware to your Kernel.php:

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

```
protected $routeMiddleware = [
    ...
    '2fa' => \Diadal\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']);
```

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

And you can decider 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 wil 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 Diadal\Google2FALaravel\Support\Authenticator;

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

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` file for details

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

[](#contributing)

Pull requests and issues are more than welcome.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.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

Every ~52 days

Recently: every ~64 days

Total

6

Last Release

2987d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/46f6ad613171949de528beabe6f51b5227abdb2d3c0525e0d2fd0fbe488f385d?d=identicon)[diadal](/maintainers/diadal)

---

Top Contributors

[![antonioribeiro](https://avatars.githubusercontent.com/u/3182864?v=4)](https://github.com/antonioribeiro "antonioribeiro (91 commits)")[![asbiin](https://avatars.githubusercontent.com/u/25419741?v=4)](https://github.com/asbiin "asbiin (7 commits)")[![diadal](https://avatars.githubusercontent.com/u/19895516?v=4)](https://github.com/diadal "diadal (5 commits)")[![it-can](https://avatars.githubusercontent.com/u/644288?v=4)](https://github.com/it-can "it-can (3 commits)")[![drakakisgeo](https://avatars.githubusercontent.com/u/1863506?v=4)](https://github.com/drakakisgeo "drakakisgeo (1 commits)")[![robertpcontreras-ts](https://avatars.githubusercontent.com/u/79461629?v=4)](https://github.com/robertpcontreras-ts "robertpcontreras-ts (1 commits)")

---

Tags

laravelAuthenticationTwo Factor Authenticationgoogle2fa

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/diadal-google2fa-laravel/health.svg)](https://phpackages.com/packages/diadal-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)[stephenjude/filament-two-factor-authentication

Filament Two Factor Authentication: Google 2FA + Passkey Authentication

81158.7k4](/packages/stephenjude-filament-two-factor-authentication)

PHPackages © 2026

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