2024-01-15 07:21:27 +00:00
|
|
|
<script lang="ts" setup>
|
2024-06-26 10:42:50 +00:00
|
|
|
import type { TotpRequest } from "@halo-dev/api-client";
|
2024-06-25 04:31:44 +00:00
|
|
|
import { ucApiClient } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
2024-01-15 07:21:27 +00:00
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
|
|
|
|
import { useQRCode } from "@vueuse/integrations/useQRCode";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { computed, ref } from "vue";
|
2024-07-01 06:41:16 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2024-01-15 07:21:27 +00:00
|
|
|
|
|
|
|
const queryClient = useQueryClient();
|
2024-07-01 06:41:16 +00:00
|
|
|
const { t } = useI18n();
|
2024-01-15 07:21:27 +00:00
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "close"): void;
|
|
|
|
}>();
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2024-01-15 07:21:27 +00:00
|
|
|
|
|
|
|
const { data } = useQuery({
|
|
|
|
queryKey: ["totp-auth-link"],
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await ucApiClient.security.twoFactor.getTotpAuthLink();
|
2024-01-15 07:21:27 +00:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const qrcode = useQRCode(computed(() => data.value?.authLink || ""));
|
|
|
|
|
|
|
|
const { mutate, isLoading } = useMutation({
|
|
|
|
mutationKey: ["configure-totp"],
|
|
|
|
mutationFn: async ({ totpRequest }: { totpRequest: TotpRequest }) => {
|
2024-06-25 04:31:44 +00:00
|
|
|
await ucApiClient.security.twoFactor.configurerTotp({
|
2024-01-15 07:21:27 +00:00
|
|
|
totpRequest: totpRequest,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSuccess() {
|
2024-07-01 06:41:16 +00:00
|
|
|
Toast.success(t("core.common.toast.save_success"));
|
2024-01-15 07:21:27 +00:00
|
|
|
queryClient.invalidateQueries({ queryKey: ["two-factor-settings"] });
|
2024-05-27 08:56:57 +00:00
|
|
|
modal.value?.close();
|
2024-01-15 07:21:27 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function onSubmit(data: TotpRequest) {
|
|
|
|
mutate({ totpRequest: data });
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<VModal
|
|
|
|
ref="modal"
|
|
|
|
:width="500"
|
|
|
|
:centered="false"
|
2024-07-01 06:41:16 +00:00
|
|
|
:title="$t('core.uc_profile.2fa.methods.totp.operations.configure.title')"
|
2024-01-15 07:21:27 +00:00
|
|
|
@close="emit('close')"
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<div class="mb-4 space-y-3 border-b border-gray-100 pb-4 text-gray-900">
|
2024-07-01 06:41:16 +00:00
|
|
|
<div class="text-sm font-semibold">
|
|
|
|
{{
|
|
|
|
$t(
|
|
|
|
"core.uc_profile.2fa.methods.totp.operations.configure.fields.qrcode.label"
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
</div>
|
2024-01-15 07:21:27 +00:00
|
|
|
<img :src="qrcode" class="rounded-base border border-gray-100" />
|
|
|
|
<details>
|
|
|
|
<summary class="cursor-pointer select-none text-sm text-gray-800">
|
2024-07-01 06:41:16 +00:00
|
|
|
{{
|
|
|
|
$t(
|
|
|
|
"core.uc_profile.2fa.methods.totp.operations.configure.fields.manual.label"
|
|
|
|
)
|
|
|
|
}}
|
2024-01-15 07:21:27 +00:00
|
|
|
</summary>
|
|
|
|
<div class="mt-3 rounded-base border border-gray-100 p-2">
|
|
|
|
<span class="text-sm text-gray-600">
|
2024-07-01 06:41:16 +00:00
|
|
|
{{
|
|
|
|
$t(
|
|
|
|
"core.uc_profile.2fa.methods.totp.operations.configure.fields.manual.help"
|
|
|
|
)
|
|
|
|
}}
|
2024-01-15 07:21:27 +00:00
|
|
|
</span>
|
|
|
|
<div class="mt-2">
|
|
|
|
<code
|
|
|
|
class="select-all rounded bg-gray-200 p-1 text-xs text-gray-900"
|
|
|
|
>
|
|
|
|
{{ data?.rawSecret }}
|
|
|
|
</code>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</details>
|
|
|
|
</div>
|
|
|
|
<FormKit id="totp-form" type="form" name="totp-form" @submit="onSubmit">
|
|
|
|
<FormKit
|
|
|
|
type="number"
|
|
|
|
name="code"
|
2024-07-01 06:41:16 +00:00
|
|
|
:label="
|
|
|
|
$t(
|
|
|
|
'core.uc_profile.2fa.methods.totp.operations.configure.fields.code.label'
|
|
|
|
)
|
|
|
|
"
|
2024-01-15 07:21:27 +00:00
|
|
|
validation="required"
|
2024-07-01 06:41:16 +00:00
|
|
|
:help="
|
|
|
|
$t(
|
|
|
|
'core.uc_profile.2fa.methods.totp.operations.configure.fields.code.help'
|
|
|
|
)
|
|
|
|
"
|
2024-01-15 07:21:27 +00:00
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
|
|
|
type="password"
|
2024-07-01 06:41:16 +00:00
|
|
|
:label="
|
|
|
|
$t(
|
|
|
|
'core.uc_profile.2fa.methods.totp.operations.configure.fields.password.label'
|
|
|
|
)
|
|
|
|
"
|
2024-01-15 07:21:27 +00:00
|
|
|
validation="required"
|
|
|
|
name="password"
|
2024-07-01 06:41:16 +00:00
|
|
|
:help="
|
|
|
|
$t(
|
|
|
|
'core.uc_profile.2fa.methods.totp.operations.configure.fields.password.help'
|
|
|
|
)
|
|
|
|
"
|
2024-01-15 07:21:27 +00:00
|
|
|
autocomplete="current-password"
|
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
|
|
|
:model-value="data?.rawSecret"
|
|
|
|
type="hidden"
|
|
|
|
name="secret"
|
|
|
|
></FormKit>
|
|
|
|
</FormKit>
|
|
|
|
</div>
|
|
|
|
<template #footer>
|
|
|
|
<VSpace>
|
|
|
|
<VButton
|
|
|
|
:loading="isLoading"
|
|
|
|
type="secondary"
|
|
|
|
@click="$formkit.submit('totp-form')"
|
|
|
|
>
|
2024-07-01 06:41:16 +00:00
|
|
|
{{ $t("core.common.buttons.save") }}
|
|
|
|
</VButton>
|
|
|
|
<VButton @click="modal?.close()">
|
|
|
|
{{ $t("core.common.buttons.close") }}
|
2024-01-15 07:21:27 +00:00
|
|
|
</VButton>
|
|
|
|
</VSpace>
|
|
|
|
</template>
|
|
|
|
</VModal>
|
|
|
|
</template>
|