PHPackages                             rezkonline/laravel-2fa - 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. rezkonline/laravel-2fa

ActiveLibrary

rezkonline/laravel-2fa
======================

A simple 2FA authentication for laravel applications.

119PHP

Since Feb 9Pushed 4y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

 [![](laravel-2fa-readme.png)](laravel-2fa-readme.png)

Laravel 2fa
===========

[](#laravel-2fa)

A simple two factor authentication for laravel applications.

 [![Total Downloads](https://camo.githubusercontent.com/df3c54c36fd43683bdb5a8bf04e15f2faa10f834ca1643b3fdc0394fa536e3cc/68747470733a2f2f706f7365722e707567782e6f72672f72657a6b6f6e6c696e652f6c61726176656c2d3266612f642f746f74616c2e737667)](https://packagist.org/packages/rezkonline/laravel-2fa) [![Latest Stable Version](https://camo.githubusercontent.com/44d75f5ab77827fde6531bc8de3dd374d755f18be005aeb96d0de5bade688bfd/68747470733a2f2f706f7365722e707567782e6f72672f72657a6b6f6e6c696e652f6c61726176656c2d3266612f762f737461626c652e737667)](https://packagist.org/packages/rezkonline/laravel-2fa) [![License](https://camo.githubusercontent.com/7d5f0527d3e017d7e4dcce6c3cd2203678aba2a2506c0dd157dbb4cdad165ab2/68747470733a2f2f706f7365722e707567782e6f72672f72657a6b6f6e6c696e652f6c61726176656c2d3266612f6c6963656e73652e737667)](https://packagist.org/packages/rezkonline/laravel-2fa) [![](https://camo.githubusercontent.com/7d68e28a9b9ef4967e8413bd3bf86acef24f7ee18ce28927e4bbfd24e0177284/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3235323138323931302f736869656c643f7374796c653d666c6174)](https://github.styleci.io/repos/252182910) [ ![](https://github.com/rezkonline/laravel-2fa/workflows/Continuous%20Integration/badge.svg) ](https://github.com/rezkonline/laravel-2fa/actions?query=workflow%3A%22Continuous+Integration%22)

- [Installation](#installation)
    - [Require via composer](#require-this-package-via-composer)
    - [Update database](#update-database-with-php-artisan-migrate)
    - [Replace authentication trait on LoginController](#replace-authenticatesusers-trait-on-logincontroller)
    - [Publish package config](#publish-package-config)
    - [Publish package assets](#publish-package-assets)
- [Usage](#usage)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

Installation
------------

[](#installation)

### Require this package via composer

[](#require-this-package-via-composer)

To get started with Laravel 2FA, use Composer to add the package to your project's dependencies:

```
composer require rezkonline/laravel-2fa
```

Or add this line in your composer.json, inside of the require section:

```
{
    "require": {
        "rezkonline/laravel-2fa": "^1.1",
    }
}
```

then run `composer install`.

### Update database with php artisan migrate

[](#update-database-with-php-artisan-migrate)

After installing the package, you must run `php artisan migrate` to add the two factor authentication fields to your `users` table.

It will add the following columns to your database table:

```
|-------- users --------|
|    two_factor_code    |
| two_factor_expires_at |
|-----------------------|

```

### Replace AuthenticatesUsers trait on LoginController

[](#replace-authenticatesusers-trait-on-logincontroller)

After that, open your `app\Http\Controllers\Auth\LoginController` file and replace the `AuthenticatesUsers` trait with the `AuthenticateUsersWithTwoFactor`, provided by this package.

Basically, it overrides the `authenticated` method on `AuthenticatesUsers`:

```
trait AuthenticateUsersWithTwoFactor
{
    use AuthenticatesUsers;

    /**
     * The user has been successfully authenticated.
     * @param Request $request
     * @param $user
     */
    public function authenticated(Request $request, $user)
    {
        $user->generateTwoFactorCode();
        $user->notify(new TwoFactorCode());
    }
}
```

Then, just use the `HasTwoFactorAuthentication` trait in your `User` model:

```
