buckhamduffy / laravel-two-factor
This is my package laravel-two-factor
Fund package maintenance!
BuckhamDuffy
Installs: 2 330
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 3
Requires
- php: ^8.2
- bacon/bacon-qr-code: ^2.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- pragmarx/google2fa-qrcode: ^3.0
- pragmarx/recovery: ^0.2.1
- spatie/laravel-data: ^3.0|^4.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- buckhamduffy/coding-standards: ^3
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.5
- pestphp/pest-plugin-laravel: ^2.0
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-04-30 03:25:54 UTC
README
An opinionated two factor authentication package for Laravel.
Installation
You can install the package via composer:
composer require buckhamduffy/laravel-two-factor
You can publish and run the migrations with:
php artisan vendor:publish --tag="two-factor-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="two-factor-config"
This is the contents of the published config file:
return [
];
Optionally, you can publish the views using
php artisan vendor:publish --tag="two-factor-views"
Add the trait and interface to the user model
use BuckhamDuffy\LaravelTwoFactor\Traits\HasTwoFactor; use BuckhamDuffy\LaravelTwoFactor\Interfaces\HasTwoFactorInterface; class User extends Model implements HasTwoFactorInterface { use HasTwoFactor; }
Add the middleware to your Kernel.php
protected $middlewareAliases = [ // ... '2fa' => \BuckhamDuffy\LaravelTwoFactor\Http\Middleware\TwoFactorMiddleware::class, ];
Route::middleware('2fa')->group(function(){ // Your routes here });
SMS (Not Implemented Yet)
When a code is requested via SMS, an event will be dispatched that you can listen for to send the SMS. You can listen for the TwoFactorCodeRequested
event and send the SMS using your preferred SMS provider.
use \BuckhamDuffy\LaravelTwoFactor\Events\TwoFactorCodeRequested; class EventProvider extends ServiceProvider { protected $listen = [ // ... TwoFactorCodeRequested::class => [ \App\Listeners\SendTwoFactorCode::class, ], ]; }
namespace App\Listeners; use BuckhamDuffy\LaravelTwoFactor\Events\TwoFactorCodeRequested; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class SendTwoFactorCode implements ShouldQueue { use InteractsWithQueue; public function handle(TwoFactorCodeRequested $event): void { $user = $event->getUser(); $user->sendTwoFactorSms($event->getCode()); } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.