2FA (Two-Factor Authentication)
Laravel Auth: 2FA (Two-Factor Authentication)
Categories:
Google2FA for 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
- antonioribeiro/google2fa-laravel: A One Time Password Authentication package, compatible with Google Authenticator for Laravel
- antonioribeiro/google2fa-qrcode: QRCode for Google2FA
- chillerlan/php-qrcode: A QR code generator and reader with a user friendly API. PHP 7.4+
- Bacon/BaconQrCode: QR Code Generator for PHP
- Get Code 2FA Auth Online - 2Fa Auth
- [筆記]Google2FA for Laravel. 一、簡述 | by Ming | TWJOIN 哲煜科技 | 台灣 | 客製化軟體應用解決方案供應者