1. Go to this page and download the library: Download olusegun171/laravel-mfa 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/ */
olusegun171 / laravel-mfa example snippets
use Olusegun171\TwoFactor\Traits\HasTwoFactor;
class User extends Authenticatable
{
use HasTwoFactor;
}
TwoFactor::remainingRecoveryCodes($user); // number of unused backup codes
// Model methods via HasTwoFactor trait
$user->hasTwoFactorEnabled(); // true once two_factor_confirmed_at is set
$user->hasTwoFactorPending(); // true if setup started but not yet confirmed
class User extends Authenticatable
{
use HasTwoFactor;
public function getTwoFactorIdentifier(): string
{
return $this->email;
}
}
// config/two-factor.php
return [
'issuer' => env('MFA_ISSUER', null), // shown in authenticator apps; defaults to app name
'totp' => [
'digits' => 6,
'period' => 30, // seconds per time-step
'window' => 1, // ±1 period tolerance for clock drift
'algorithm' => 'sha1',
],
];
$data = TwoFactor::generate($user);
// Pass $data to your view:
// $data['qr_code_url'] — <img src="{{ $data['qr_code_url'] }}">
// $data['secret'] — manual entry fallback
// $data['recovery_codes'] — show once, store somewhere safe
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Olusegun171\TwoFactor\Facades\TwoFactor;
$user = User::where('email', $request->email)->first();
if (!$user || !Hash::check($request->password, $user->password)) {
return back()->withErrors(['email' => 'Invalid credentials.']);
}
if (TwoFactor::