PHP code example of elgibor-solution / laravel-2fa
1. Go to this page and download the library: Download elgibor-solution/laravel-2fa 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/ */
elgibor-solution / laravel-2fa example snippets
use Illuminate\Support\Facades\App;
// SMS
App::singleton('2fa.sender.sms', function () {
return function (string $to, string $message, array $context = []) {
// Integrate your SMS gateway here
// Example: Sms::to($to)->send($message);
};
});
// WhatsApp
App::singleton('2fa.sender.whatsapp', function () {
return function (string $to, string $message, array $context = []) {
// Integrate your WhatsApp provider here
// Example: WhatsApp::sendText($to, $message);
};
});
use TwoFA;
// Start TOTP enrollment
$resp = TwoFA::initTotp($user->id); // ['secret' => '...', 'otpauth_uri' => '...']
// Confirm and enable TOTP
$ok = TwoFA::confirmTotp($user->id, $request->code);
// Disable 2FA
TwoFA::disable($user->id);
// Create a self-generated code and deliver it
$res = TwoFA::createSelfGenerated($user->id, 'sms', $user->phone, 'login');
// Validate a code (works for TOTP or self-generated depending on user settings)
$isValid = TwoFA::validate($user->id, $request->code, 'login');