feat(KAN-37): rebuild security and application key settings
This commit is contained in:
@@ -1,297 +1,261 @@
|
||||
<script setup lang="ts">
|
||||
import { Form } from '@inertiajs/vue3';
|
||||
import { useClipboard } from '@vueuse/core';
|
||||
import { useForm } from '@inertiajs/vue3';
|
||||
import { Check, Copy, ScanLine } from 'lucide-vue-next';
|
||||
import { computed, nextTick, ref, useTemplateRef, watch } from 'vue';
|
||||
import AlertError from '@/components/AlertError.vue';
|
||||
import AuthFormFeedback from '@/components/AuthFormFeedback.vue';
|
||||
import InputError from '@/components/InputError.vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
InputOTP,
|
||||
InputOTPGroup,
|
||||
InputOTPSlot,
|
||||
} from '@/components/ui/input-otp';
|
||||
import { Spinner } from '@/components/ui/spinner';
|
||||
import { useAppearance } from '@/composables/useAppearance';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { useFormProtection } from '@/composables/useFormProtection';
|
||||
import { useLocalization } from '@/composables/useLocalization';
|
||||
import { useTwoFactorAuth } from '@/composables/useTwoFactorAuth';
|
||||
import { confirm } from '@/routes/two-factor';
|
||||
import type { TwoFactorConfigContent } from '@/types';
|
||||
|
||||
type Props = {
|
||||
const props = defineProps<{
|
||||
requiresConfirmation: boolean;
|
||||
twoFactorEnabled: boolean;
|
||||
};
|
||||
|
||||
const { resolvedAppearance } = useAppearance();
|
||||
|
||||
const props = defineProps<Props>();
|
||||
}>();
|
||||
const isOpen = defineModel<boolean>('isOpen');
|
||||
const { t } = useLocalization();
|
||||
const {
|
||||
qrCodeSvg,
|
||||
manualSetupKey,
|
||||
hasSetupData,
|
||||
clearSetupData,
|
||||
fetchSetupData,
|
||||
errors,
|
||||
loading,
|
||||
} = useTwoFactorAuth();
|
||||
const form = useForm({ code: '' }).dontRemember('code');
|
||||
const protection = useFormProtection(form, { isDirty: () => false });
|
||||
const verification = ref(false);
|
||||
const copied = ref(false);
|
||||
const copyError = ref(false);
|
||||
const codeInput = useTemplateRef('codeInput');
|
||||
const title = computed(() =>
|
||||
t(
|
||||
props.twoFactorEnabled
|
||||
? 'security.setupEnabled'
|
||||
: verification.value
|
||||
? 'security.verifyTitle'
|
||||
: 'security.setupTitle',
|
||||
),
|
||||
);
|
||||
const description = computed(() =>
|
||||
t(
|
||||
props.twoFactorEnabled
|
||||
? 'security.setupEnabledDescription'
|
||||
: verification.value
|
||||
? 'security.verifyDescription'
|
||||
: 'security.setupDescription',
|
||||
),
|
||||
);
|
||||
|
||||
const { copy, copied } = useClipboard();
|
||||
const { qrCodeSvg, manualSetupKey, clearSetupData, fetchSetupData, errors } =
|
||||
useTwoFactorAuth();
|
||||
|
||||
const showVerificationStep = ref(false);
|
||||
const code = ref<string>('');
|
||||
|
||||
const pinInputContainerRef = useTemplateRef('pinInputContainerRef');
|
||||
|
||||
const modalConfig = computed<TwoFactorConfigContent>(() => {
|
||||
if (props.twoFactorEnabled) {
|
||||
return {
|
||||
title: 'Two-factor authentication enabled',
|
||||
description:
|
||||
'Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app.',
|
||||
buttonText: 'Close',
|
||||
};
|
||||
function close(open: boolean): void {
|
||||
if (!form.processing) {
|
||||
isOpen.value = open;
|
||||
}
|
||||
|
||||
if (showVerificationStep.value) {
|
||||
return {
|
||||
title: 'Verify authentication code',
|
||||
description: 'Enter the 6-digit code from your authenticator app',
|
||||
buttonText: 'Continue',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
title: 'Enable two-factor authentication',
|
||||
description:
|
||||
'To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app',
|
||||
buttonText: 'Continue',
|
||||
};
|
||||
});
|
||||
|
||||
const handleModalNextStep = () => {
|
||||
if (props.requiresConfirmation) {
|
||||
showVerificationStep.value = true;
|
||||
|
||||
nextTick(() => {
|
||||
pinInputContainerRef.value?.querySelector('input')?.focus();
|
||||
});
|
||||
|
||||
}
|
||||
async function next(): Promise<void> {
|
||||
if (props.twoFactorEnabled || !props.requiresConfirmation) {
|
||||
close(false);
|
||||
return;
|
||||
}
|
||||
|
||||
clearSetupData();
|
||||
isOpen.value = false;
|
||||
};
|
||||
|
||||
const resetModalState = () => {
|
||||
if (props.twoFactorEnabled) {
|
||||
clearSetupData();
|
||||
verification.value = true;
|
||||
await nextTick();
|
||||
codeInput.value?.$el?.focus();
|
||||
}
|
||||
function submit(): void {
|
||||
protection.submit(confirm(), {
|
||||
preserveScroll: true,
|
||||
errorBag: 'confirmTwoFactorAuthentication',
|
||||
onSuccess: () => {
|
||||
isOpen.value = false;
|
||||
},
|
||||
onFinish: () => {
|
||||
form.reset();
|
||||
},
|
||||
});
|
||||
}
|
||||
async function copyKey(): Promise<void> {
|
||||
const key = manualSetupKey.value;
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
|
||||
showVerificationStep.value = false;
|
||||
code.value = '';
|
||||
};
|
||||
|
||||
watch(
|
||||
() => isOpen.value,
|
||||
async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
resetModalState();
|
||||
|
||||
return;
|
||||
copyError.value = false;
|
||||
try {
|
||||
await navigator.clipboard.writeText(key);
|
||||
if (isOpen.value && manualSetupKey.value === key) {
|
||||
copied.value = true;
|
||||
}
|
||||
|
||||
if (!qrCodeSvg.value) {
|
||||
await fetchSetupData();
|
||||
} catch {
|
||||
if (isOpen.value) {
|
||||
copyError.value = true;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
watch(isOpen, (open) => {
|
||||
verification.value = false;
|
||||
copied.value = false;
|
||||
copyError.value = false;
|
||||
form.resetAndClearErrors();
|
||||
protection.clearFeedback();
|
||||
clearSetupData();
|
||||
if (open) {
|
||||
void fetchSetupData();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Dialog :open="isOpen" @update:open="isOpen = $event">
|
||||
<DialogContent class="sm:max-w-md">
|
||||
<DialogHeader class="flex items-center justify-center">
|
||||
<div
|
||||
class="border-border bg-card mb-3 w-auto rounded-full border p-0.5 shadow-sm"
|
||||
<Dialog :open="isOpen" @update:open="close">
|
||||
<DialogContent
|
||||
class="sm:max-w-md"
|
||||
:show-close-button="!form.processing"
|
||||
@escape-key-down="form.processing && $event.preventDefault()"
|
||||
@interact-outside="form.processing && $event.preventDefault()"
|
||||
>
|
||||
<DialogHeader>
|
||||
<ScanLine class="text-primary size-8" />
|
||||
<DialogTitle>{{ title }}</DialogTitle>
|
||||
<DialogDescription>{{ description }}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<template v-if="!verification">
|
||||
<p
|
||||
v-if="loading"
|
||||
role="status"
|
||||
class="bg-muted animate-pulse rounded-lg p-6 text-center"
|
||||
>
|
||||
{{ t('security.loadingSetup') }}
|
||||
</p>
|
||||
<div v-else-if="errors.length" class="gap-stack grid">
|
||||
<AlertError :errors="errors" />
|
||||
<Button variant="outline" @click="fetchSetupData">{{
|
||||
t('common.retry')
|
||||
}}</Button>
|
||||
</div>
|
||||
<div v-else-if="hasSetupData" class="gap-column grid min-w-0">
|
||||
<div
|
||||
class="border-border bg-muted relative overflow-hidden rounded-full border p-2.5"
|
||||
>
|
||||
<div
|
||||
class="absolute inset-0 grid grid-cols-5 opacity-50"
|
||||
>
|
||||
<div
|
||||
v-for="i in 5"
|
||||
:key="`col-${i}`"
|
||||
class="border-border border-r last:border-r-0"
|
||||
class="mx-auto aspect-square w-full max-w-56 rounded-lg border bg-white p-4 text-black"
|
||||
v-html="qrCodeSvg"
|
||||
/>
|
||||
<div class="grid min-w-0 gap-2">
|
||||
<Label for="setup-key">{{
|
||||
t('security.manualKey')
|
||||
}}</Label>
|
||||
<div class="flex min-w-0 gap-2">
|
||||
<Input
|
||||
id="setup-key"
|
||||
:model-value="manualSetupKey"
|
||||
readonly
|
||||
class="min-w-0 font-mono text-xs"
|
||||
/>
|
||||
<Button
|
||||
variant="outline"
|
||||
:aria-label="
|
||||
t(
|
||||
copied
|
||||
? 'security.copied'
|
||||
: 'security.copyKey',
|
||||
)
|
||||
"
|
||||
@click="copyKey"
|
||||
><Check v-if="copied" /><Copy v-else
|
||||
/></Button>
|
||||
</div>
|
||||
<div
|
||||
class="absolute inset-0 grid grid-rows-5 opacity-50"
|
||||
<p
|
||||
v-if="copied"
|
||||
role="status"
|
||||
class="text-caption text-success-foreground"
|
||||
>
|
||||
<div
|
||||
v-for="i in 5"
|
||||
:key="`row-${i}`"
|
||||
class="border-border border-b last:border-b-0"
|
||||
/>
|
||||
</div>
|
||||
<ScanLine
|
||||
class="text-foreground relative z-20 size-6"
|
||||
{{ t('security.copied') }}
|
||||
</p>
|
||||
<InputError
|
||||
v-if="copyError"
|
||||
:message="t('security.copyError')"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogTitle>{{ modalConfig.title }}</DialogTitle>
|
||||
<DialogDescription class="text-center">
|
||||
{{ modalConfig.description }}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div
|
||||
class="relative flex w-auto flex-col items-center justify-center space-y-5"
|
||||
>
|
||||
<template v-if="!showVerificationStep">
|
||||
<AlertError v-if="errors?.length" :errors="errors" />
|
||||
<template v-else>
|
||||
<div
|
||||
class="relative mx-auto flex max-w-md items-center overflow-hidden"
|
||||
>
|
||||
<div
|
||||
class="border-border relative mx-auto aspect-square w-64 overflow-hidden rounded-lg border"
|
||||
>
|
||||
<div
|
||||
v-if="!qrCodeSvg"
|
||||
class="bg-background absolute inset-0 z-10 flex aspect-square h-auto w-full animate-pulse items-center justify-center"
|
||||
>
|
||||
<Spinner class="size-6" />
|
||||
</div>
|
||||
<div
|
||||
v-else
|
||||
class="relative z-10 overflow-hidden border p-5"
|
||||
>
|
||||
<div
|
||||
v-html="qrCodeSvg"
|
||||
class="flex aspect-square size-full items-center justify-center"
|
||||
:style="{
|
||||
filter:
|
||||
resolvedAppearance === 'dark'
|
||||
? 'invert(1) brightness(1.5)'
|
||||
: undefined,
|
||||
}"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex w-full items-center space-x-5">
|
||||
<Button class="w-full" @click="handleModalNextStep">
|
||||
{{ modalConfig.buttonText }}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="relative flex w-full items-center justify-center"
|
||||
>
|
||||
<div
|
||||
class="bg-border absolute inset-0 top-1/2 h-px w-full"
|
||||
/>
|
||||
<span class="bg-card relative px-2 py-1"
|
||||
>or, enter the code manually</span
|
||||
>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="flex w-full items-center justify-center space-x-2"
|
||||
>
|
||||
<div
|
||||
class="border-border flex w-full items-stretch overflow-hidden rounded-xl border"
|
||||
>
|
||||
<div
|
||||
v-if="!manualSetupKey"
|
||||
class="bg-muted flex h-full w-full items-center justify-center p-3"
|
||||
>
|
||||
<Spinner />
|
||||
</div>
|
||||
<template v-else>
|
||||
<input
|
||||
type="text"
|
||||
readonly
|
||||
:value="manualSetupKey"
|
||||
class="bg-background text-foreground h-full w-full p-3"
|
||||
/>
|
||||
<button
|
||||
@click="copy(manualSetupKey || '')"
|
||||
class="border-border hover:bg-muted relative block h-auto border-l px-3"
|
||||
>
|
||||
<Check
|
||||
v-if="copied"
|
||||
class="w-4 text-green-500"
|
||||
/>
|
||||
<Copy v-else class="w-4" />
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<Form
|
||||
v-bind="confirm.form()"
|
||||
error-bag="confirmTwoFactorAuthentication"
|
||||
reset-on-error
|
||||
@finish="code = ''"
|
||||
@success="isOpen = false"
|
||||
v-slot="{ errors, processing }"
|
||||
<p
|
||||
v-if="requiresConfirmation && !twoFactorEnabled"
|
||||
class="text-caption text-muted-foreground"
|
||||
>
|
||||
{{ t('security.setupPendingDescription') }}
|
||||
</p>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" @click="close(false)">{{
|
||||
t('common.close')
|
||||
}}</Button>
|
||||
<Button v-if="hasSetupData" @click="next">{{
|
||||
t(
|
||||
twoFactorEnabled || !requiresConfirmation
|
||||
? 'common.done'
|
||||
: 'security.continue',
|
||||
)
|
||||
}}</Button>
|
||||
</DialogFooter>
|
||||
</template>
|
||||
<form v-else class="gap-column grid" @submit.prevent="submit">
|
||||
<AuthFormFeedback
|
||||
:state="protection.state.value"
|
||||
:response-status="protection.responseStatus.value"
|
||||
@retry="submit"
|
||||
/>
|
||||
<div class="grid gap-2">
|
||||
<Label for="authentication-code">{{
|
||||
t('security.authenticationCode')
|
||||
}}</Label>
|
||||
<Input
|
||||
id="authentication-code"
|
||||
ref="codeInput"
|
||||
v-model="form.code"
|
||||
name="code"
|
||||
type="text"
|
||||
inputmode="numeric"
|
||||
autocomplete="one-time-code"
|
||||
pattern="[0-9]{6}"
|
||||
maxlength="6"
|
||||
required
|
||||
:disabled="form.processing"
|
||||
:aria-invalid="!!form.errors.code"
|
||||
aria-describedby="authentication-code-error"
|
||||
class="font-mono text-lg tracking-widest"
|
||||
/>
|
||||
<InputError
|
||||
id="authentication-code-error"
|
||||
:message="form.errors.code"
|
||||
/>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
:disabled="form.processing"
|
||||
@click="
|
||||
verification = false;
|
||||
form.resetAndClearErrors();
|
||||
protection.clearFeedback();
|
||||
"
|
||||
>{{ t('common.back') }}</Button
|
||||
>
|
||||
<input type="hidden" name="code" :value="code" />
|
||||
<div
|
||||
ref="pinInputContainerRef"
|
||||
class="relative w-full space-y-3"
|
||||
>
|
||||
<div
|
||||
class="flex w-full flex-col items-center justify-center space-y-3 py-2"
|
||||
>
|
||||
<InputOTP
|
||||
id="otp"
|
||||
v-model="code"
|
||||
:maxlength="6"
|
||||
:disabled="processing"
|
||||
>
|
||||
<InputOTPGroup>
|
||||
<InputOTPSlot
|
||||
v-for="index in 6"
|
||||
:key="index"
|
||||
:index="index - 1"
|
||||
/>
|
||||
</InputOTPGroup>
|
||||
</InputOTP>
|
||||
<InputError :message="errors?.code" />
|
||||
</div>
|
||||
|
||||
<div class="flex w-full items-center space-x-5">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
class="w-auto flex-1"
|
||||
@click="showVerificationStep = false"
|
||||
:disabled="processing"
|
||||
>
|
||||
Back
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
class="w-auto flex-1"
|
||||
:disabled="processing || code.length < 6"
|
||||
>
|
||||
Confirm
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
</template>
|
||||
</div>
|
||||
<Button
|
||||
type="submit"
|
||||
:disabled="form.code.length !== 6"
|
||||
:loading="form.processing"
|
||||
>{{ t('security.confirm') }}</Button
|
||||
>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user