Skip to content

Commit 14f1165

Browse files
committed
refactor: streamline TwoFactor components, simplify form handling, and improve UI logic consistency
1 parent cae3c55 commit 14f1165

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

app/Http/Controllers/Settings/TwoFactorAuthenticationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function show(Request $request): Response
2222

2323
return Inertia::render('settings/TwoFactor', [
2424
'requiresConfirmation' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'),
25-
'twoFactorEnabled' => filled($request->user()->two_factor_confirmed_at),
25+
'twoFactorEnabled' => $request->user()->hasEnabledTwoFactorAuthentication(),
2626
]);
2727
}
2828
}

resources/js/pages/settings/TwoFactor.vue

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,15 @@ const breadcrumbs = [
2929
},
3030
];
3131
32-
const twoFactorEnabling = ref(false);
3332
const qrCodeSvg = ref<string | null>(null);
3433
const manualSetupKey = ref<string | null>(null);
3534
const isTwoFactorSetupModalOpen = ref(false);
3635
const isInVerificationStep = ref(false);
3736
38-
const enableTwoFactorAuthentication = (): void => {
39-
router.post(
40-
route('two-factor.enable'),
41-
{},
42-
{
43-
preserveScroll: true,
44-
onBefore: () => {
45-
twoFactorEnabling.value = true;
46-
},
47-
onSuccess: () =>
48-
Promise.all([fetchQRCode(), fetchManualSetupKey(), fetchRecoveryCodes()]).then(() => {
49-
isTwoFactorSetupModalOpen.value = true;
50-
}),
51-
onFinish: () => {
52-
twoFactorEnabling.value = false;
53-
},
54-
},
55-
);
37+
const enableTwoFactorAuthenticationSuccess = (): void => {
38+
Promise.all([fetchQRCode(), fetchManualSetupKey(), fetchRecoveryCodes()]).then(() => {
39+
isTwoFactorSetupModalOpen.value = true;
40+
});
5641
};
5742
5843
const fetchQRCode = async () => {
@@ -71,7 +56,6 @@ const code = ref<number[]>([]);
7156
const pinInputContainerRef = ref<HTMLElement | null>(null);
7257
const verificationCode: ComputedRef<string> = computed(() => code.value.join(''));
7358
74-
7559
const recoveryCodesList = ref<string[]>([]);
7660
const showingRecoveryCodes = ref(false);
7761
@@ -87,18 +71,20 @@ const toggleRecoveryCodesVisibility = () => {
8771
showingRecoveryCodes.value = !showingRecoveryCodes.value;
8872
};
8973
74+
const disableTwoFactorAuthenticationSuccess = (): void => {
75+
isInVerificationStep.value = false;
76+
isTwoFactorSetupModalOpen.value = false;
77+
qrCodeSvg.value = null;
78+
manualSetupKey.value = null;
79+
recoveryCodesList.value = [];
80+
code.value = [];
81+
};
9082
9183
const disableTwoFactorAuthentication = (): void => {
9284
router.delete(route('two-factor.disable'), {
9385
preserveScroll: true,
94-
onSuccess: () => {
95-
isInVerificationStep.value = false;
96-
isTwoFactorSetupModalOpen.value = false;
97-
qrCodeSvg.value = null;
98-
manualSetupKey.value = null;
99-
recoveryCodesList.value = [];
100-
code.value = [];
101-
}
86+
async: false,
87+
onSuccess: () => disableTwoFactorAuthenticationSuccess(),
10288
});
10389
};
10490
@@ -122,14 +108,18 @@ const copyToClipboard = (text: string): void => {
122108
When you enable 2FA, you'll be prompted for a secure code during login, which can be retrieved from your phone's TOTP
123109
supported app.
124110
</p>
125-
<Dialog
126-
:open="isTwoFactorSetupModalOpen"
127-
@update:open="(value) => { isTwoFactorSetupModalOpen = value}"
128-
>
111+
<Dialog v-model:open="isTwoFactorSetupModalOpen" :close="() => disableTwoFactorAuthentication()">
129112
<DialogTrigger as-child>
130-
<Button type="button" @click="enableTwoFactorAuthentication" :disabled="twoFactorEnabling">
131-
{{ twoFactorEnabling ? 'Enabling...' : 'Enable' }}
132-
</Button>
113+
<Form
114+
:action="route('two-factor.enable')"
115+
method="post"
116+
@success="enableTwoFactorAuthenticationSuccess"
117+
#default="{ processing }"
118+
>
119+
<Button type="submit" :disabled="processing">
120+
{{ processing ? 'Enabling...' : 'Enable' }}
121+
</Button>
122+
</Form>
133123
</DialogTrigger>
134124
<DialogContent class="sm:max-w-md">
135125
<DialogHeader class="flex items-center justify-center">
@@ -149,20 +139,12 @@ const copyToClipboard = (text: string): void => {
149139
</div>
150140
</div>
151141
<DialogTitle>
152-
{{
153-
!isInVerificationStep
154-
? requiresConfirmation
155-
? 'Finish Enabling 2-step Verification'
156-
: 'Turn on 2-step Verification'
157-
: 'Verify Authentication Code'
158-
}}
142+
{{ !isInVerificationStep ? 'Finish Enabling 2-step Verification' : 'Verify Authentication Code' }}
159143
</DialogTitle>
160144
<DialogDescription class="text-center">
161145
{{
162146
!isInVerificationStep
163-
? requiresConfirmation
164-
? 'To finish enabling two factor authentication, scan the QR code or enter the setup key'
165-
: 'Open your authenticator app and choose Scan QR code'
147+
? 'To finish enabling two factor authentication, scan the QR code or enter the setup key'
166148
: 'Enter the 6-digit code from your authenticator app'
167149
}}
168150
</DialogDescription>
@@ -204,7 +186,7 @@ const copyToClipboard = (text: string): void => {
204186
}
205187
"
206188
>
207-
{{ requiresConfirmation ? 'Continue to Verify' : 'Continue' }}
189+
{{ requiresConfirmation ? 'Continue to Verify' : 'Finish Setup' }}
208190
</Button>
209191
</div>
210192

@@ -238,11 +220,7 @@ const copyToClipboard = (text: string): void => {
238220
</template>
239221

240222
<template v-else>
241-
<Form
242-
:action="route('two-factor.confirm')"
243-
method="post"
244-
#default="{ error, processing }"
245-
>
223+
<Form :action="route('two-factor.confirm')" method="post" #default="{ error, processing }">
246224
<input type="hidden" name="code" :value="code" />
247225
<div ref="pinInputContainerRef" class="relative w-full space-y-3">
248226
<div class="flex w-full flex-col items-center justify-center space-y-3 py-2">
@@ -350,7 +328,7 @@ const copyToClipboard = (text: string): void => {
350328
</div>
351329

352330
<div class="relative inline">
353-
<Form :action="route('two-factor.disable')" method="delete" #default="{ processing }">
331+
<Form :action="route('two-factor.disable')" :async="false" method="delete" #default="{ processing }">
354332
<Button variant="destructive" type="submit" :disabled="processing">
355333
{{ processing ? 'Disabling...' : 'Disable 2FA' }}
356334
</Button>

0 commit comments

Comments
 (0)