PHP code example of ongudidan / laravel-multi-2fa

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);
})

return [
    // Frontend stack: 'auto', 'inertia', 'blade', or 'api'
    'stack' => 'auto',

    // Static feature toggles or runtime fallback
    'features' => [
        'email_otp' => true,
        'authenticator' => true,
        'passkey' => true,
    ],

    // Custom resolver to read dynamic DB settings (e.g. system settings table)
    'settings_resolver' => \Ongudidan\Multi2fa\Services\DefaultSettingsResolver::class,

    // Expiry in minutes for email OTP
    'otp_expiry_minutes' => 10,

    // Built-in routes
    'routes' => [
        'enabled' => true,
        'middleware' => ['web'],
    ],

    // View component mappings for each stack
    'views' => [
        'inertia' => [
            'email_otp' => 'auth/EmailOtpLogin',
            'login_challenge' => 'auth/LoginChallengeSelection',
            'two_factor_settings' => 'settings/TwoFactor',
            'passkeys_settings' => 'settings/Passkeys',
        ],
        'blade' => [
            'email_otp' => 'multi-2fa::auth.email-otp',
            'login_challenge' => 'multi-2fa::auth.challenge-selection',
            'two_factor_settings' => 'multi-2fa::settings.two-factor',
            'passkeys_settings' => 'multi-2fa::settings.passkeys',
        ],
    ],

    // Redirect after login
    'home' => null, // defaults to config('fortify.home') or /dashboard
];
bash
php artisan vendor:publish --tag=multi-2fa-config
bash
  php artisan vendor:publish --tag=multi-2fa-vue
  
bash
  php artisan vendor:publish --tag=multi-2fa-blade
  
bash
php artisan migrate