PHP code example of roundly-consulting / two-factor-for-laravel

1. Go to this page and download the library: Download roundly-consulting/two-factor-for-laravel 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/ */

    

roundly-consulting / two-factor-for-laravel example snippets


use Illuminate\Foundation\Auth\User as Authenticatable;
use RoundlyConsulting\TwoFactor\Concerns\HasTwoFactorAuthentication;
use RoundlyConsulting\TwoFactor\Contracts\TwoFactorAuthenticatable;

final class User extends Authenticatable implements TwoFactorAuthenticatable
{
    use HasTwoFactorAuthentication;

    protected function casts(): array
    {
        return [...$this->twoFactorCasts()];
    }
}

use RoundlyConsulting\TwoFactor\Facades\TwoFactor;

$setup = TwoFactor::for($user)->start(issuer: 'Acme');
$setup->provisioningUri;                          // otpauth://totp/Acme:… — render it as a QR code
$setup->recoveryCodes;                            // list<string> — show these once

TwoFactor::for($user)->confirm($code);            // the first authenticator code switches 2FA on

$result = TwoFactor::for($user)->attempt($code);  // TOTP (replay-safe) or a single-use recovery code
$result->verified;                                // bool
$result->method;                                  // TwoFactorMethod::Totp | ::RecoveryCode | null

TwoFactor::for($user)->status()->enabled;         // true
bash
composer vendor:publish --tag="two-factor-migrations"
php artisan migrate