Skip to content

Commit ea4d231

Browse files
committed
refactor: simplify two-factor authentication
1 parent ee18d0f commit ea4d231

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace App\Http\Controllers\Settings;
64

75
use App\Http\Controllers\Controller;
8-
use App\Models\User;
96
use Illuminate\Http\Request;
107
use Inertia\Inertia;
118
use Inertia\Response;
129

1310
class TwoFactorAuthenticationController extends Controller
1411
{
12+
/**
13+
* Show the user's two-factor authentication settings page.
14+
*/
1515
public function show(Request $request): Response
1616
{
1717
$user = $request->user();
18-
$confirmed = ! is_null($user->two_factor_confirmed_at);
19-
$hasTwoFactorSecret = ! is_null($user->two_factor_secret);
18+
19+
$confirmed = filled($user->two_factor_confirmed_at);
20+
$hasSecret = filled($user->two_factor_secret);
21+
$showSetup = ! $confirmed && $hasSecret;
22+
23+
$recoveryCodes = $hasSecret && filled($user->two_factor_recovery_codes)
24+
? (json_decode(decrypt($user->two_factor_recovery_codes), true) ?: [])
25+
: [];
2026

2127
return Inertia::render('settings/TwoFactor', [
2228
'confirmed' => $confirmed,
23-
'recoveryCodes' => $hasTwoFactorSecret ? json_decode(decrypt($user->two_factor_recovery_codes)) : [],
24-
'qrCodeSvg' => !$confirmed && $hasTwoFactorSecret ? $user->twoFactorQrCodeSvg() : null,
25-
'secretKey' => !$confirmed && $hasTwoFactorSecret ? decrypt($user->two_factor_secret) : null,
29+
'recoveryCodes' => $recoveryCodes,
30+
'qrCodeSvg' => $showSetup ? $user->twoFactorQrCodeSvg() : null,
31+
'secretKey' => $showSetup ? decrypt($user->two_factor_secret) : null,
2632
]);
2733
}
2834
}

tests/Feature/Settings/TwoFactorAuthenticationTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Tests\Feature\Settings;
64

75
use App\Models\User;

0 commit comments

Comments
 (0)