PHP code example of buckhamduffy / laravel-two-factor
1. Go to this page and download the library: Download buckhamduffy/laravel-two-factor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
buckhamduffy / laravel-two-factor example snippets
return [
];
use BuckhamDuffy\LaravelTwoFactor\Traits\HasTwoFactor;
use BuckhamDuffy\LaravelTwoFactor\Interfaces\HasTwoFactorInterface;
class User extends Model implements HasTwoFactorInterface {
use HasTwoFactor;
}
Route::middleware('2fa')->group(function(){
// Your routes here
});
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());
}
}
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use BuckhamDuffy\LaravelTwoFactor\Traits\CustomThrottlesLogins;
class LoginController extends Controller
{
use AuthenticatesUsers;
use CustomThrottlesLogins {
CustomThrottlesLogins::hasTooManyLoginAttempts insteadof AuthenticatesUsers;
CustomThrottlesLogins::incrementLoginAttempts insteadof AuthenticatesUsers;
CustomThrottlesLogins::clearLoginAttempts insteadof AuthenticatesUsers;
CustomThrottlesLogins::sendLockoutResponse insteadof AuthenticatesUsers;
}
}