1. Go to this page and download the library: Download darkghosthunter/laraguard 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/ */
darkghosthunter / laraguard example snippets
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DarkGhostHunter\Laraguard\TwoFactorAuthentication;
use DarkGhostHunter\Laraguard\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', [
'as_qr_code' => $secret->toQr(), // As QR Code
'as_uri' => $secret->toUri(), // As "otpauth://" URI.
'as_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();
} else {
return 'Try again!';
}
}
use Illuminate\Http\Request;
public function showRecoveryCodes(Request $request)
{
return $request->user()->generateRecoveryCodes();
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use DarkGhostHunter\Laraguard\Laraguard;
public function login(Request $request)
{
// ...
$credentials = $request->only('email', 'password');
if (Auth::attemptWhen($credentials, Laraguard::hasCode(), $request->filled('remember'))) {
return redirect()->home();
}
return back()->withErrors(['email' => 'Bad credentials'])
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use DarkGhostHunter\Laraguard\Laraguard;
public function login(Request $request)
{
// ...
$credentials = $request->only('email', 'password');
if (Auth::attemptWhen($credentials, Laraguard::hasCodeOrFails(), $request->filled('remember'))) {
return redirect()->home();
}
return back()->withErrors(['email', 'Authentication failed!']);
}
public function disableTwoFactorAuth(Request $request)
{
$request->user()->disableTwoFactorAuth();
return 'Two-Factor Authentication has been disabled!';
}