1. Go to this page and download the library: Download michaeldzjap/twofactor-auth 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/ */
michaeldzjap / twofactor-auth example snippets
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RemovePrimaryFromTwoFactorAuthsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('two_factor_auths', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::table('two_factor_auths', function (Blueprint $table) {
$table->unsignedInteger('user_id')->change();
$table->dropPrimary(['user_id']);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('two_factor_auths', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::table('two_factor_auths', function (Blueprint $table) {
$table->increments('user_id')->change();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
}
...
use MichaelDzjap\TwoFactorAuth\TwoFactorAuthenticable;
class User extends Authenticatable
{
use Notifiable, TwoFactorAuthenticable;
...
$router->group([
'middleware' => ['web', 'guest'],
'namespace' => 'App\Http\Controllers\Auth',
], function () use ($router) {
$router->get('/auth/token', 'TwoFactorAuthController@showTwoFactorForm')->name('auth.token');
$router->post('/auth/token', 'TwoFactorAuthController@verifyToken');
});
...
use MichaelDzjap\TwoFactorAuth\Contracts\TwoFactorProvider;
class LoginController extends Controller
{
...
/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
if (resolve(TwoFactorProvider::class)->enabled($user)) {
return self::startTwoFactorAuthProcess($request, $user);
}
return redirect()->intended($this->redirectPath());
}
/**
* Log out the user and start the two factor authentication state.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\User $user
* @return \Illuminate\Http\Response
*/
private function startTwoFactorAuthProcess(Request $request, $user)
{
// Logout user, but remember user id
auth()->logout();
$request->session()->put(
'two-factor:auth', array_merge(['id' => $user->id], $request->only('email', 'remember'))
);
self::registerUserAndSendToken($user);
return redirect()->route('auth.token');
}
/**
* Provider specific two-factor authentication logic. In the case of MessageBird
* we just want to send an authentication token via SMS.
*
* @param \App\Models\User $user
* @return mixed
*/
private function registerUserAndSendToken(User $user)
{
// Custom, provider dependend logic for sending an authentication token
// to the user. In the case of MessageBird Verify this could simply be
// resolve(TwoFactorProvider::class)->sendSMSToken($this->user)
// Here we assume this function is called from a queue'd job
dispatch(new SendSMSToken($user));
}
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use MichaelDzjap\TwoFactorAuth\Http\Controllers\TwoFactorAuthenticatesUsers;
class TwoFactorAuthController extends Controller
{
use TwoFactorAuthenticatesUsers;
/**
* The maximum number of attempts to allow.
*
* @var int
*/
protected $maxAttempts = 5;
/**
* The number of minutes to throttle for.
*
* @var int
*/
protected $decayMinutes = 1;
/**
* Where to redirect users after two-factor authentication passes.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
}
...
<form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
@csrf
{{-- Add this block to show an error message in case of an expired token or user lockout --}}
@if ($errors->has('token'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>{{ $errors->first('token') }}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
...
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use MichaelDzjap\TwoFactorAuth\Http\Controllers\TwoFactorAuthenticatesUsers;
class TwoFactorAuthController extends Controller
{
use TwoFactorAuthenticatesUsers;
/**
* Where to redirect users after two-factor authentication passes.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Where to redirect users after two-factor authentication fails.
*
* @var string
*/
protected $redirectToAfterFailure = '/login';
}
resolve(\MichaelDzjap\TwoFactorAuth\TwoFactorAuthManager)->extend('dummy', function ($app) {
return new DummyProvider;
});
...
'dummy' => [
'driver' => 'dummy',
],
...
shell
php artisan vendor:publish
shell
php artisan migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.