Laravel Two Factor Authentication
Two Factor Authentication for Laravel 5.3+, (*1)
, (*2)
Installation
Add the package to your project using Composer:, (*3)
composer require whyounes/laravel-two-factor-auth
Publish package assets:, (*4)
php artisan vendor:publish
Run the migrations:, (*5)
php artisan migrate
Add it to you providers list:, (*6)
// config/app.php
// ...
'providers' => [
// ...
Whyounes\TFAuth\TwoFAProvider::class,
};
Add the TFAuthTrait trait to your user model:, (*7)
// app/User.php
class User extends Authenticatable
{
use \Whyounes\TFAuth\Models\TFAuthTrait;
// ...
}
Configurations
There are only two configurations that you can set:, (*8)
-
delete_verification_code_after_auth: Set it to true if you want to delete unused verification codes after login.
-
verification_code_length: How long the verification code is.
Verification Code Sender
By default, the package uses Twilio to send verification codes (SMS and Phone). You can easily change it like this:, (*9)
use Whyounes\TFAuth\Contracts\VerificationCodeSenderInterface;
class MyService implements VerificationCodeSenderInterface
{
public function sendCodeViaSMS($code, $phone, $message = "Your verification code is %s")
{
// Send code and return boolean for status
}
public function sendCodeViaPhone($code, $phone, $message = "Your verification code is %s")
{
// Send code and return boolean for status
}
}
Next we should switch implementation in the container:, (*10)
use Whyounes\TFAuth\Contracts\VerificationCodeSenderInterface;
class AppProvider extends ServiceProvider
{
public function register()
{
// ...
$this->app->bind(VerificationCodeSenderInterface::class, MyService::class);
}
}
That's it, your new service is going to be used for sending verification codes. If you add a new service implementation, you can submit a new pull request and I'll add it to the package :), (*11)
Example
Check this repository for a demo application using the package., (*12)