1. Go to this page and download the library: Download ongudidan/laravel-multi-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/ */
ongudidan / laravel-multi-2fa example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Passkeys\Contracts\PasskeyUser;
use Laravel\Passkeys\PasskeyAuthenticatable;
class User extends Authenticatable implements PasskeyUser
{
use TwoFactorAuthenticatable, PasskeyAuthenticatable;
protected $hidden = [
'password',
'remember_token',
'two_factor_secret',
'two_factor_recovery_codes',
];
}
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Features;
use Illuminate\Http\Request;
use Ongudidan\Multi2fa\Actions\RedirectIfEmailOtpRequired;
use Ongudidan\Multi2fa\Actions\RedirectIfTwoFactorAuthenticatable;
use Ongudidan\Multi2fa\Services\TwoFactorService;
public function boot(): void
{
Fortify::authenticateThrough(function (Request $request) {
return [
config('fortify.limiters.login') ? null : \Laravel\Fortify\Actions\EnsureLoginIsNotThrottled::class,
config('fortify.lowercase_usernames') ? \Laravel\Fortify\Actions\CanonicalizeUsername::class : null,
RedirectIfEmailOtpRequired::class,
RedirectIfTwoFactorAuthenticatable::class,
\Laravel\Fortify\Actions\AttemptToAuthenticate::class,
\Laravel\Fortify\Actions\PrepareAuthenticatedSession::class,
];
});
// Configure the challenge view to support switching between methods:
Fortify::twoFactorChallengeView(function (Request $request) {
$userId = $request->session()->get('login.id');
$canSwitch = false;
if ($userId) {
$twoFactorService = app(TwoFactorService::class);
$userModel = config('auth.providers.users.model', \App\Models\User::class);
$user = $userModel::find($userId);
if ($user) {
$canSwitch = $twoFactorService->canSwitchMethods($user, 'authenticator');
}
}
// Uses renderView to automatically support Inertia or Blade:
return app(TwoFactorService::class)->renderView($request, 'two_factor_challenge', [
'canSwitch' => $canSwitch,
]);
});
}
use Ongudidan\Multi2fa\Services\TwoFactorService;
public function share(Request $request): array
{
return [
...parent::share($request),
'security_settings' => app(TwoFactorService::class)->getSecuritySettings(),
];
}
use Ongudidan\Multi2fa\Http\Middleware\EnsureTwoFactorEnabledGlobally;
->withMiddleware(function (Middleware $middleware) {
$middleware->append(EnsureTwoFactorEnabledGlobally::class);
})