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\TwoFactorServiceProvider::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 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!';
}

// Require 2FA to be turned on before reaching these routes.
Route::middleware('2fa.enabled')->group(function () {
    Route::get('/billing', BillingController::class);
});

// Ask for a fresh 2FA code before reaching these routes, even if already logged in.
Route::middleware('2fa.confirm')->group(function () {
    Route::get('/account/danger-zone', DangerZoneController::class);
});

$request->validate(['code' => ['
shell
php artisan two-factor-auth:install
shell
php artisan migrate