Two-factor authentication package for Laravel 7.x and 8.x via e-mail.
This package provides you to set of middleware, view, migration, routes with controllers, transaltions and methods to user model which allow you to set up 2FA vie email for your application.
- First install package via composer. Run
composer require stanfortonski/laravel-twofactor
. - Setup provider. In config/app.php add following code to bottom of providers:
'providers' => [
//...
Stanfortonski\Laraveltwofactor\ServiceProvider::class
],
- Setup middleware. In app/Http/Kernel.php add following code to bottom of middlewares:
protected $routeMiddleware = [
//...
'twofactor' => \Stanfortonski\Laraveltwofactor\Middleware\TwoFactor::class
];
- You have to publish config file twofactor.php. Run command:
php artisan vendor:publish --provider="Stanfortonski\Laraveltwofactor\ServiceProvider"
. - Run
php artisan migrate
. - Add line
$user->startTwoFactor()
in your login process. For example in Laravel/ui add this line in Auth\LoginController@authenticated method before redirect. - Set up your email in .env.
In config/twofactor.php
- If you want to disable twofactor/preferences routes set
preferences.allow
to false. - If you want to change expire time of 2FA code you will change
expire_duration
. Default value is 15 minutes. - Look at routes.login, routes.successful and routes.return that must be real routes name in your application. More in file itself.
You have to set twofactor middleware in routes. The way depends on you.
Example:
Route::get('/home', function(){
return view('home');
})->middleware(['auth', 'twofactor']);