From f485b1c6b01522b5f7db7a9844f1880d1885d18c Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Fri, 12 Sep 2025 13:19:10 +0530 Subject: [PATCH 1/3] Add an error state in useTwoFactorAuth.ts --- resources/js/composables/useTwoFactorAuth.ts | 41 ++++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/resources/js/composables/useTwoFactorAuth.ts b/resources/js/composables/useTwoFactorAuth.ts index 70db708c..3e8b96aa 100644 --- a/resources/js/composables/useTwoFactorAuth.ts +++ b/resources/js/composables/useTwoFactorAuth.ts @@ -13,52 +13,67 @@ const fetchJson = async (url: string): Promise => { return response.json(); }; -const qrCodeSvg = ref(null); +const errors = ref([]); const manualSetupKey = ref(null); +const qrCodeSvg = ref(null); const recoveryCodesList = ref([]); const hasSetupData = computed(() => qrCodeSvg.value !== null && manualSetupKey.value !== null); export const useTwoFactorAuth = () => { const fetchQrCode = async (): Promise => { - const { svg } = await fetchJson<{ svg: string; url: string }>(qrCode.url()); + try { + const { svg } = await fetchJson<{ svg: string; url: string }>(qrCode.url()); - qrCodeSvg.value = svg; + qrCodeSvg.value = svg; + } catch { + errors.value.push('Failed to fetch QR code'); + qrCodeSvg.value = null; + } }; const fetchSetupKey = async (): Promise => { - const { secretKey: key } = await fetchJson<{ secretKey: string }>(secretKey.url()); + try { + const { secretKey: key } = await fetchJson<{ secretKey: string }>(secretKey.url()); - manualSetupKey.value = key; + manualSetupKey.value = key; + } catch { + errors.value.push('Failed to fetch a setup key'); + manualSetupKey.value = null; + } }; const clearSetupData = (): void => { manualSetupKey.value = null; qrCodeSvg.value = null; + clearErrors(); + }; + + const clearErrors = (): void => { + errors.value = []; }; const clearTwoFactorAuthData = (): void => { clearSetupData(); - + clearErrors(); recoveryCodesList.value = []; }; const fetchRecoveryCodes = async (): Promise => { try { + clearErrors(); recoveryCodesList.value = await fetchJson(recoveryCodes.url()); - } catch (error) { - console.error('Failed to fetch recovery codes:', error); - + } catch { + errors.value.push('Failed to fetch recovery codes'); recoveryCodesList.value = []; } }; const fetchSetupData = async (): Promise => { try { + clearErrors(); await Promise.all([fetchQrCode(), fetchSetupKey()]); - } catch (error) { - console.error('Failed to fetch setup data:', error); - + } catch { qrCodeSvg.value = null; manualSetupKey.value = null; } @@ -68,8 +83,10 @@ export const useTwoFactorAuth = () => { qrCodeSvg, manualSetupKey, recoveryCodesList, + errors, hasSetupData, clearSetupData, + clearErrors, clearTwoFactorAuthData, fetchQrCode, fetchSetupKey, From a071b28e1708b9ce121753a012f417ffbb1f6ce9 Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Fri, 19 Sep 2025 17:07:38 +0530 Subject: [PATCH 2/3] Display errors in UI --- resources/js/components/AlertError.vue | 30 +++++ .../js/components/TwoFactorRecoveryCodes.vue | 10 +- .../js/components/TwoFactorSetupModal.vue | 114 +++++++++--------- resources/js/components/ui/alert/Alert.vue | 21 ++++ .../components/ui/alert/AlertDescription.vue | 17 +++ .../js/components/ui/alert/AlertTitle.vue | 17 +++ resources/js/components/ui/alert/index.ts | 24 ++++ 7 files changed, 175 insertions(+), 58 deletions(-) create mode 100644 resources/js/components/AlertError.vue create mode 100644 resources/js/components/ui/alert/Alert.vue create mode 100644 resources/js/components/ui/alert/AlertDescription.vue create mode 100644 resources/js/components/ui/alert/AlertTitle.vue create mode 100644 resources/js/components/ui/alert/index.ts diff --git a/resources/js/components/AlertError.vue b/resources/js/components/AlertError.vue new file mode 100644 index 00000000..2170e5a6 --- /dev/null +++ b/resources/js/components/AlertError.vue @@ -0,0 +1,30 @@ + + + diff --git a/resources/js/components/TwoFactorRecoveryCodes.vue b/resources/js/components/TwoFactorRecoveryCodes.vue index 972aabd2..0ceb0b23 100644 --- a/resources/js/components/TwoFactorRecoveryCodes.vue +++ b/resources/js/components/TwoFactorRecoveryCodes.vue @@ -1,4 +1,5 @@ + + diff --git a/resources/js/components/ui/alert/AlertDescription.vue b/resources/js/components/ui/alert/AlertDescription.vue new file mode 100644 index 00000000..9f7d24df --- /dev/null +++ b/resources/js/components/ui/alert/AlertDescription.vue @@ -0,0 +1,17 @@ + + + diff --git a/resources/js/components/ui/alert/AlertTitle.vue b/resources/js/components/ui/alert/AlertTitle.vue new file mode 100644 index 00000000..b2183843 --- /dev/null +++ b/resources/js/components/ui/alert/AlertTitle.vue @@ -0,0 +1,17 @@ + + + diff --git a/resources/js/components/ui/alert/index.ts b/resources/js/components/ui/alert/index.ts new file mode 100644 index 00000000..42d07b64 --- /dev/null +++ b/resources/js/components/ui/alert/index.ts @@ -0,0 +1,24 @@ +import type { VariantProps } from "class-variance-authority" +import { cva } from "class-variance-authority" + +export { default as Alert } from "./Alert.vue" +export { default as AlertDescription } from "./AlertDescription.vue" +export { default as AlertTitle } from "./AlertTitle.vue" + +export const alertVariants = cva( + "relative w-full rounded-lg border px-4 py-3 text-sm grid has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] grid-cols-[0_1fr] has-[>svg]:gap-x-3 gap-y-0.5 items-start [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current", + { + variants: { + variant: { + default: "bg-card text-card-foreground", + destructive: + "text-destructive bg-card [&>svg]:text-current *:data-[slot=alert-description]:text-destructive/90", + }, + }, + defaultVariants: { + variant: "default", + }, + }, +) + +export type AlertVariants = VariantProps From 519ecfdbca91cf3dda2ec202b3aaf94966d66085 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Fri, 19 Sep 2025 09:37:45 -0400 Subject: [PATCH 3/3] fix styles --- resources/js/components/TwoFactorRecoveryCodes.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/js/components/TwoFactorRecoveryCodes.vue b/resources/js/components/TwoFactorRecoveryCodes.vue index 0ceb0b23..6490740b 100644 --- a/resources/js/components/TwoFactorRecoveryCodes.vue +++ b/resources/js/components/TwoFactorRecoveryCodes.vue @@ -39,7 +39,7 @@ onMounted(async () => {