PHP code example of jmrashed / two-factor-auth

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

    

jmrashed / two-factor-auth example snippets


'providers' => [
    // Other Service Providers
    Jmrashed\TwoFactorAuth\TwoFactorAuthServiceProvider::class,
],

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Jmrashed\TwoFactorAuth\TwoFactorAuthentication;
use Jmrashed\TwoFactorAuth\Contracts\TwoFactorAuthenticatable;

class User extends Authenticatable implements TwoFactorAuthenticatable
{
    use TwoFactorAuthentication;

    // Additional model code...
}

use Illuminate\Http\Request;

public function prepareTwoFactor(Request $request)
{
    $secret = $request->user()->createTwoFactorAuth();

    return view('user.2fa', [
        'qr_code' => $secret->toQr(),     // QR Code
        'uri'     => $secret->toUri(),    // "otpauth://" URI
        'string'  => $secret->toString(), // Secret as string
    ]);
}

use Illuminate\Http\Request;

public function confirmTwoFactor(Request $request)
{
    $request->validate([
        'code' => '

use Illuminate\Http\Request;

public function confirmTwoFactor(Request $request)
{
    if ($request->user()->confirmTwoFactorAuth($request->code)) {
        return $request->user()->getRecoveryCodes();
    }
    return 'Try again!';
}

use Illuminate\Http\Request;

public function showRecoveryCodes(Request $request)
{
    return $request->user()->generateRecoveryCodes();
}

use Jmrashed\TwoFactorAuth\Facades\Auth2FA;
use Illuminate\Http\Request;

public function login(Request $request)
{
    // Validate user credentials
     $attempt = Auth2FA::attempt($request->only('email', 'password'));
    if ($attempt) {
        return 'You are logged in!';
    }
    return 'Hey, you should make an account!';
}
shell
php artisan two-factor-auth:install
shell
php artisan migrate