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

ActiveLibrary

airondev/laravel-2fa
====================

This package allow you to enable two-factor authentication in your Laravel applications. It stores tokens locally and notify users about their token via mail, SMS or any custom channel. Includes native conditionnal check to trigger or not 2FA, using known devices, IP addresses or IP locations.

07PHP

Since Feb 6Pushed 2y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Two-Factor Authentication
=================================

[](#laravel-two-factor-authentication)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Latest Version on Packagist](https://camo.githubusercontent.com/c3467ce2e6f7f0efb82291eb2d588ba7bb190825b70f7b01b25243fe4ecb58f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4169726f6e6465762d6167656e63792f6c61726176656c2d3266612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Airondev-agency/laravel-2fa)[![Total Downloads](https://camo.githubusercontent.com/b68391345481f74c2a65a4c0a6cd3844a732b08d58c4d80e273bdf62c046ab4d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4169726f6e6465762d6167656e63792f6c61726176656c2d3266612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Airondev-agency/laravel-2fa)

- [Introduction](#introduction)
- [Installation](#installation)
- [Configuration](#configuration)
    - [Built-in](#configuration-builtin)
    - [Custom Notification](#configuration-custom-notification)
    - [Custom Policies](#configuration-custom-policies)
    - [Custom Drivers](#configuration-custom-drivers)
- [Contribute](#contribute)

Introduction
------------

[](#introduction)

This package allow you to enable two-factor authentication in your Laravel applications very easily, without the need to add middleware or any modification to your routes. It stores tokens in your database in a distinct table, so you don't need to alter your `users` table. Notify users about their token via mail, SMS or any custom channel.

Includes native conditionnal check to trigger or not 2FA : you may skip the check when the user is using a known browser, IP address, IP Geo location, or any [custom rule](#configuration-custom-policies).

This package was inspired by the [srmklive/laravel-twofactor-authentication](https://github.com/srmklive/laravel-twofactor-authentication) package, which supports the [Authy](https://authy.com) 2FA auth.

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

[](#installation)

1. Use composer to install the package :

```
composer require Airondev-agency/laravel-2fa
```

2. Add the service provider to your `providers` array in `config/app.php` file like so:

```
'providers' => [
    [...]
    /*
     * Package Service Providers...
     */
    Airondev\Laravel2FA\Laravel2FAServiceProvider::class,
],
```

3. Run the following command to publish assets :

```
php artisan vendor:publish --provider "Airondev\Laravel2FA\Laravel2FAServiceProvider"
```

4. Run the following command to migrate database :

```
php artisan migrate
```

5. Add the following lines in your User model (e.g `App\Models\User.php`)

- Before the class declaration, add these lines:

```
use Airondev\Laravel2FA\TwoFactorAuthenticatable;
use Airondev\Laravel2FA\Contracts\TwoFactorAuthenticatableContract;
```

- Alter the class definition to implements the `TwoFactorAuthenticatableContract` contract :

```
class User extends Authenticatable implements AuthenticatableContract,
                                              AuthorizableContract,
                                              CanResetPasswordContract,
                                              TwoFactorAuthenticatableContract
```

- Add the `TwoFactorAuthenticatable` trait :

```
use Authenticatable,
    Authorizable,
    CanResetPassword,
    TwoFactorAuthenticatable;
```

6. Make sure your user model is using the [Notifiable trait](https://laravel.com/docs/8.x/notifications#using-the-notifiable-trait).
7. You need to change the login workflow by adding the `authenticated` method to your `app\Http\Controllers\Auth\LoginController.php` class.

```
