2FA (Two-Factor Authentication)

Laravel Auth: 2FA (Two-Factor Authentication)

Google2FA for Laravel

https://github.com/antonioribeiro/google2fa-laravel

Install

# 2FA Package
composer require pragmarx/google2fa-laravel
# Generate QRcode
composer require pragmarx/google2fa-qrcode
# Generate QRcode image library
composer require chillerlan/php-qrcode

Publish the config file

php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"

Middleware

// app/Http/Kernel.php
protected $routeMiddleware = [
    '2fa' => \PragmaRX\Google2FALaravel\Middleware::class,
];

Generate 2FA Secret & Photo

Google2FA::setQRCodeBackend('svg');
$secret_2fa = Google2FA::generateSecretKey();

$secret_2fa_qrcode_url = Google2FA::getQRCodeInline(
    'Laravel App',
    $UserModel->email,
    $secret_2fa
);
<!-- Blade -->
<p>
    2FA Secret: {{ $secret_2fa }}
</p>
<p >
    <img src="{{$secret_2fa_qrcode_url}}" alt="" style="height: 80vh;">
</p>

Verify 2FA secret & OTP

// Generate current OTP (one time password)
$one_time_password = Google2FA::getCurrentOtp($secret_2fa);
// Verify 2FA secret
$is_2fa_verify = Google2FA::verifyKey($secret_2fa, $one_time_password);

Reference