Skip to content

Commit

Permalink
Merge pull request #6 from thedevdojo/tnylea/add-two-factor-auth-cont…
Browse files Browse the repository at this point in the history
…roller

Add TwoFactorAuthenticationController with code confirmation methods
  • Loading branch information
tnylea authored May 8, 2024
2 parents 6d95641 + 049f97c commit 60af34c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Http/Controllers/TwoFactorAuthenticationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Devdojo\Auth\Http\Controllers;

use Illuminate\Http\Request;
use PragmaRX\Google2FA\Google2FA;
use App\Models\User;

class TwoFactorAuthenticationController extends Controller
{
protected $google2fa;

public function __construct(Google2FA $google2fa)
{
$this->google2fa = $google2fa;
}

public function confirmAuthenticationCode(Request $request)
{
$user = $request->user();
$valid = $this->google2fa->verifyKey($user->two_factor_secret, $request->code);

if ($valid) {
// Handle successful authentication
} else {
// Handle failed authentication
}
}

public function confirmRecoveryCode(Request $request)
{
$user = $request->user();
$recoveryCodes = $user->two_factor_recovery_codes;
$valid = in_array($request->code, $recoveryCodes);

if ($valid) {
// Handle successful recovery code confirmation
} else {
// Handle failed recovery code confirmation
}
}
}

0 comments on commit 60af34c

Please sign in to comment.