1. Go to this page and download the library: Download laragear/two-factor 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/ */
laragear / two-factor example snippets
use Illuminate\Http\Request;
use Laragear\TwoFactor\Facades\Auth2FA;
public function login(Request $request)
{
$attempt = Auth2FA::attempt($request->only('email', 'password'));
if ($attempt) {
return 'You are logged in!';
}
return 'Hey, you should make an account!';
}
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laragear\TwoFactor\TwoFactorAuthentication;
use Laragear\TwoFactor\Contracts\TwoFactorAuthenticatable;
class User extends Authenticatable implements TwoFactorAuthenticatable
{
use TwoFactorAuthentication;
// ...
}
use Illuminate\Http\Request;
public function prepareTwoFactor(Request $request)
{
$secret = $request->user()->createTwoFactorAuth();
return view('user.2fa', [
'qr_code' => $secret->toQr(), // As QR Code
'uri' => $secret->toUri(), // As "otpauth://" URI.
'string' => $secret->toString(), // As a 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 Laragear\TwoFactor\Models\TwoFactorAuthentication;
use MyRandomGenerator;
$generator = function ($length, $iteration, $amount) {
return MyRandomGenerator::random($length)->make();
}
TwoFactorAuthentication::generateRecoveryCodesUsing($generator);
use Laragear\TwoFactor\Facades\Auth2FA;
use Illuminate\Http\Request;
public function login(Request $request)
{
// If the user is trying for the first time, ensure both email and the password are
// 'password' => 'il' => 'There is no existing user for these credentials']);
}
use Laragear\TwoFactor\Facades\Auth2FA;
Auth2FA::message('You need 2FA set up to access this area')
->redirect('/auth/2fa-
use Illuminate\Support\Facades\Auth;
use Laragear\TwoFactor\TwoFactor;
$attempt = Auth::attemptWhen(
[/* Credentials... */], TwoFactor::hasCode(), $request->filled('remember')
);
use Illuminate\Http\Request;
public function changeServerSetting(Request $request)
{
if ($request->user()->wasTwoFactorBypassedBySafeDevice()) {
// Do something ...
}
// ...
}
public function disableTwoFactorAuth(Request $request)
{
$request->user()->disableTwoFactorAuth();
return 'Two-Factor Authentication has been disabled!';
}
Route::get('system/settings', function () {
// ...
})->middleware('2fa.enabled');
use Illuminate\Support\Facades\Route;
Route::view('2fa-;
Route::get('api/token', function () {
// ...
})->middleware('2fa.confirm');
Route::post('api/token/delete', function () {
// ...
})->middleware('2fa.confirm');
use Illuminate\Support\Facades\Route;
use Laragear\TwoFactor\Http\Controllers\ConfirmTwoFactorCodeController;
Route::get('2fa-confirm', [ConfirmTwoFactorCodeController::class, 'form'])
->name('2fa.confirm');
Route::post('2fa-confirm', [ConfirmTwoFactorCodeController::class, 'confirm']);
use Illuminate\Support\Facades\Route;
Route::get('api/token', function () {
// ...
})->middleware('2fa.
use Illuminate\Support\Facades\Route;
Route::get('api/token', function () {
// ...
})->middleware('2fa.edirect-route-name,true');
public function checkTotp(Request $request)
{
$request->validate([
'code' => 'totp'
]);
// ...
}
public function disableTwoFactorAuth()
{
// ...
session()->flash('message', trans('two-factor::messages.success'));
return back();
}
public function getTwoFactorIssuer(): string
{
return request()->getHost();
}
public function getTwoFactorUserIdentifier(): string
{
return request()->getHost() === 'admin.myapp.com'
? $this->getAttribute('name')
: $this->getAttribute('email');
}
use Illuminate\Database\Schema\Blueprint;
use Laragear\TwoFactor\Migrations\TwoFactorAuthenticationMigration;
return new class extends TwoFactorAuthenticationMigration
{
/**
* Add additional columns to the table
*/
public function addCustomColumns(Blueprint $table): void
{
// Here you can add custom columns to the Two Factor table.
//
// $table->string('alias')->nullable();
}
};
use Illuminate\Database\Schema\Blueprint;
public function afterUp(Blueprint $table)
{
$table->foreignId('authenticatable_id')->references('id')->on('users');
}
public function beforeDown(Blueprint $table)
{
$table->dropForeign('authenticatable_id');
}
use Illuminate\Database\Schema\Blueprint;
use Laragear\TwoFactor\Migrations\TwoFactorAuthenticationMigration;
return new class extends TwoFactorAuthenticationMigration
{
protected string $morphsType = 'ulid'
};
use Laragear\TwoFactor\Models\TwoFactorAuthentication;
public function register(): void
{
TwoFactorAuthentication::$useTable = 'my_custom_table';
}