<?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