whyounes/laravel-two-factor-auth

Laravel two factor authentication

v1.0.0 2016-12-21 15:36 UTC

This package is auto-updated.

Last update: 2024-03-29 02:36:46 UTC


README

Two Factor Authentication for Laravel 5.3+

Build status 68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63356164616163382d326138352d346530352d616339352d6336333566663463386132332f6d696e692e706e67

Installation

Add the package to your project using Composer:

composer require whyounes/laravel-two-factor-auth

Publish package assets:

php artisan vendor:publish

Run the migrations:

php artisan migrate

Add it to you providers list:

// config/app.php

// ...
'providers' => [
    // ...
    Whyounes\TFAuth\TwoFAProvider::class,
};

Add the TFAuthTrait trait to your user model:

// app/User.php

class User extends Authenticatable
{
    use \Whyounes\TFAuth\Models\TFAuthTrait;

    // ...
}

Configurations

There are only two configurations that you can set:

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

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:

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

Example

Check this repository for a demo application using the package.