|
| 1 | +<script setup lang="ts"> |
| 2 | +import InputError from '@/components/InputError.vue'; |
| 3 | +import { Button } from '@/components/ui/button'; |
| 4 | +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; |
| 5 | +import { PinInput, PinInputGroup, PinInputSlot } from '@/components/ui/pin-input'; |
| 6 | +import { useClipboard } from '@/composables/useClipboard'; |
| 7 | +import { Form } from '@inertiajs/vue3'; |
| 8 | +import { Check, Copy, Loader2, ScanLine } from 'lucide-vue-next'; |
| 9 | +import { computed, nextTick, ref, watch } from 'vue'; |
| 10 | +
|
| 11 | +interface Props { |
| 12 | + isOpen: boolean; |
| 13 | + requiresConfirmation: boolean; |
| 14 | + twoFactorEnabled: boolean; |
| 15 | +} |
| 16 | +
|
| 17 | +interface Emits { |
| 18 | + (e: 'update:isOpen', value: boolean): void; |
| 19 | +} |
| 20 | +
|
| 21 | +const props = defineProps<Props>(); |
| 22 | +const emit = defineEmits<Emits>(); |
| 23 | +
|
| 24 | +const { recentlyCopied, copyToClipboard } = useClipboard(); |
| 25 | +
|
| 26 | +const qrCodeSvg = ref<string | null>(null); |
| 27 | +const manualSetupKey = ref<string | null>(null); |
| 28 | +const hasSetupData = computed(() => { |
| 29 | + return qrCodeSvg.value !== null && manualSetupKey.value !== null; |
| 30 | +}); |
| 31 | +
|
| 32 | +const showVerificationStep = ref(false); |
| 33 | +const code = ref<number[]>([]); |
| 34 | +const codeValue = computed(() => code.value.join('')); |
| 35 | +
|
| 36 | +const pinInputContainerRef = ref<HTMLElement | null>(null); |
| 37 | +
|
| 38 | +const modalConfig = computed(() => { |
| 39 | + if (props.twoFactorEnabled) { |
| 40 | + return { |
| 41 | + title: 'You have enabled two factor authentication.', |
| 42 | + description: 'Two factor authentication is now enabled, scan the QR code or enter the setup key in authenticator app.', |
| 43 | + buttonText: 'Close', |
| 44 | + }; |
| 45 | + } |
| 46 | +
|
| 47 | + if (showVerificationStep.value) { |
| 48 | + return { |
| 49 | + title: 'Verify Authentication Code', |
| 50 | + description: 'Enter the 6-digit code from your authenticator app', |
| 51 | + buttonText: 'Continue', |
| 52 | + }; |
| 53 | + } |
| 54 | +
|
| 55 | + return { |
| 56 | + title: 'Turn on 2-step Verification', |
| 57 | + description: 'To finish enabling two factor authentication, scan the QR code or enter the setup key in authenticator app', |
| 58 | + buttonText: 'Continue', |
| 59 | + }; |
| 60 | +}); |
| 61 | +
|
| 62 | +const handleModalNextStep = () => { |
| 63 | + if (props.requiresConfirmation) { |
| 64 | + showVerificationStep.value = true; |
| 65 | + nextTick(() => { |
| 66 | + pinInputContainerRef.value?.querySelector('input')?.focus(); |
| 67 | + }); |
| 68 | + return; |
| 69 | + } |
| 70 | +
|
| 71 | + emit('update:isOpen', false); |
| 72 | + return; |
| 73 | +}; |
| 74 | +
|
| 75 | +const fetchSetupData = async () => { |
| 76 | + try { |
| 77 | + const [qrResponse, keyResponse] = await Promise.all([ |
| 78 | + fetch(route('two-factor.qr-code'), { headers: { Accept: 'application/json' } }), |
| 79 | + fetch(route('two-factor.secret-key'), { headers: { Accept: 'application/json' } }), |
| 80 | + ]); |
| 81 | +
|
| 82 | + const { svg } = await qrResponse.json(); |
| 83 | + const { secretKey } = await keyResponse.json(); |
| 84 | +
|
| 85 | + qrCodeSvg.value = svg; |
| 86 | + manualSetupKey.value = secretKey; |
| 87 | + } catch (error) { |
| 88 | + console.error('Failed to fetch 2FA setup data:', error); |
| 89 | + } |
| 90 | +}; |
| 91 | +
|
| 92 | +watch( |
| 93 | + () => props.isOpen, |
| 94 | + (isOpen) => { |
| 95 | + if (isOpen && !qrCodeSvg.value) { |
| 96 | + fetchSetupData(); |
| 97 | + } |
| 98 | + }, |
| 99 | +); |
| 100 | +
|
| 101 | +defineExpose({ hasSetupData }); |
| 102 | +</script> |
| 103 | + |
| 104 | +<template> |
| 105 | + <Dialog :open="isOpen" @update:open="emit('update:isOpen', $event)"> |
| 106 | + <DialogContent class="sm:max-w-md"> |
| 107 | + <DialogHeader class="flex items-center justify-center"> |
| 108 | + <div class="mb-3 w-auto rounded-full border border-border bg-card p-0.5 shadow-sm"> |
| 109 | + <div class="relative overflow-hidden rounded-full border border-border bg-muted p-2.5"> |
| 110 | + <div class="absolute inset-0 grid grid-cols-5 opacity-50"> |
| 111 | + <div v-for="i in 5" :key="`col-${i}`" class="border-r border-border last:border-r-0" /> |
| 112 | + </div> |
| 113 | + <div class="absolute inset-0 grid grid-rows-5 opacity-50"> |
| 114 | + <div v-for="i in 5" :key="`row-${i}`" class="border-b border-border last:border-b-0" /> |
| 115 | + </div> |
| 116 | + <ScanLine class="relative z-20 size-6 text-foreground" /> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + <DialogTitle>{{ modalConfig.title }}</DialogTitle> |
| 120 | + <DialogDescription class="text-center"> |
| 121 | + {{ modalConfig.description }} |
| 122 | + </DialogDescription> |
| 123 | + </DialogHeader> |
| 124 | + |
| 125 | + <div class="relative flex w-auto flex-col items-center justify-center space-y-5"> |
| 126 | + <template v-if="!showVerificationStep"> |
| 127 | + <div class="relative mx-auto flex max-w-md items-center overflow-hidden"> |
| 128 | + <div class="relative mx-auto aspect-square w-64 overflow-hidden rounded-lg border border-border"> |
| 129 | + <div |
| 130 | + v-if="!qrCodeSvg" |
| 131 | + class="absolute inset-0 z-10 flex aspect-square h-auto w-full animate-pulse items-center justify-center bg-background" |
| 132 | + > |
| 133 | + <Loader2 class="size-6 animate-spin" /> |
| 134 | + </div> |
| 135 | + <div v-else class="relative z-10 overflow-hidden border p-5"> |
| 136 | + <div v-html="qrCodeSvg" class="flex aspect-square size-full items-center justify-center" /> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + |
| 141 | + <div class="flex w-full items-center space-x-5"> |
| 142 | + <Button class="w-full" @click="handleModalNextStep"> |
| 143 | + {{ modalConfig.buttonText }} |
| 144 | + </Button> |
| 145 | + </div> |
| 146 | + |
| 147 | + <div class="relative flex w-full items-center justify-center"> |
| 148 | + <div class="absolute inset-0 top-1/2 h-px w-full bg-border" /> |
| 149 | + <span class="relative bg-card px-2 py-1">or, enter the code manually</span> |
| 150 | + </div> |
| 151 | + |
| 152 | + <div class="flex w-full items-center justify-center space-x-2"> |
| 153 | + <div class="flex w-full items-stretch overflow-hidden rounded-xl border border-border"> |
| 154 | + <div v-if="!manualSetupKey" class="flex h-full w-full items-center justify-center bg-muted p-3"> |
| 155 | + <Loader2 class="size-4 animate-spin" /> |
| 156 | + </div> |
| 157 | + <template v-else> |
| 158 | + <input type="text" readonly :value="manualSetupKey" class="h-full w-full bg-background p-3 text-foreground" /> |
| 159 | + <button |
| 160 | + @click="copyToClipboard(manualSetupKey || '')" |
| 161 | + class="relative block h-auto border-l border-border px-3 hover:bg-muted" |
| 162 | + > |
| 163 | + <Check v-if="recentlyCopied" class="w-4 text-green-500" /> |
| 164 | + <Copy v-else class="w-4" /> |
| 165 | + </button> |
| 166 | + </template> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </template> |
| 170 | + |
| 171 | + <template v-else> |
| 172 | + <Form |
| 173 | + :action="route('two-factor.confirm')" |
| 174 | + method="post" |
| 175 | + reset-on-error |
| 176 | + @error="code = []" |
| 177 | + @success="emit('update:isOpen', false)" |
| 178 | + v-slot="{ errors, processing }" |
| 179 | + > |
| 180 | + <input type="hidden" name="code" :value="codeValue" /> |
| 181 | + <div ref="pinInputContainerRef" class="relative w-full space-y-3"> |
| 182 | + <div class="flex w-full flex-col items-center justify-center space-y-3 py-2"> |
| 183 | + <PinInput id="otp" placeholder="○" v-model="code" type="number" otp> |
| 184 | + <PinInputGroup> |
| 185 | + <PinInputSlot autofocus v-for="(id, index) in 6" :key="id" :index="index" :disabled="processing" /> |
| 186 | + </PinInputGroup> |
| 187 | + </PinInput> |
| 188 | + <InputError :message="errors?.confirmTwoFactorAuthentication?.code" /> |
| 189 | + </div> |
| 190 | + |
| 191 | + <div class="flex w-full items-center space-x-5"> |
| 192 | + <Button |
| 193 | + type="button" |
| 194 | + variant="outline" |
| 195 | + class="w-auto flex-1" |
| 196 | + @click="showVerificationStep = false" |
| 197 | + :disabled="processing" |
| 198 | + > |
| 199 | + Back |
| 200 | + </Button> |
| 201 | + <Button type="submit" class="w-auto flex-1" :disabled="processing || codeValue.length < 6"> |
| 202 | + {{ processing ? 'Confirming...' : 'Confirm' }} |
| 203 | + </Button> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + </Form> |
| 207 | + </template> |
| 208 | + </div> |
| 209 | + </DialogContent> |
| 210 | + </Dialog> |
| 211 | +</template> |
0 commit comments