Files
cloudron-box/dashboard/src/components/DisableTwoFADialog.vue
T

88 lines
2.3 KiB
Vue
Raw Normal View History

2025-06-11 10:47:47 +02:00
<script setup>
2025-12-05 18:06:03 +01:00
import { ref, useTemplateRef } from 'vue';
2025-07-10 11:55:11 +02:00
import { PasswordInput, Dialog, FormGroup } from '@cloudron/pankow';
2025-11-13 16:10:27 +01:00
import ProfileModel from '../models/ProfileModel.js';
2025-06-11 10:47:47 +02:00
const emit = defineEmits([ 'success' ]);
const profileModel = ProfileModel.create();
const dialog = useTemplateRef('dialog');
const formError = ref({});
const busy = ref (false);
const password = ref('');
2025-12-05 18:06:03 +01:00
const form = useTemplateRef('form');
const isFormValid = ref(false);
function checkValidity() {
isFormValid.value = form.value ? form.value.checkValidity() : false;
}
2025-06-11 10:47:47 +02:00
async function onSubmit() {
2025-12-05 18:06:03 +01:00
if (!form.value.reportValidity()) return;
2025-06-11 10:47:47 +02:00
busy.value = true;
formError.value = {};
const [error] = await profileModel.disableTwoFA(password.value);
if (error) {
if (error.status === 412) formError.value.password = error.body.message;
else {
formError.value.generic = error.status ? error.body.message : 'Internal error';
console.error('Failed to disable 2fa', error);
}
busy.value = false;
return;
}
emit('success');
dialog.value.close();
busy.value = false;
formError.value = {};
}
defineExpose({
async open() {
password.value = '';
busy.value = false;
formError.value = {};
dialog.value.open();
2025-12-05 18:06:03 +01:00
setTimeout(checkValidity, 100); // update state of the confirm button
2025-06-11 10:47:47 +02:00
}
});
</script>
<template>
<Dialog ref="dialog"
:title="$t('profile.disable2FA.title')"
:confirm-label="$t('profile.disable2FA.disable')"
:confirm-active="!busy && isFormValid"
:confirm-busy="busy"
confirm-style="primary"
:reject-label="$t('main.dialog.cancel')"
:reject-active="!busy"
reject-style="secondary"
@confirm="onSubmit"
>
2025-12-05 18:06:03 +01:00
<form @submit.prevent="onSubmit" autocomplete="off" ref="form" @input="checkValidity()">
2025-06-11 10:47:47 +02:00
<fieldset :disabled="busy">
2025-12-05 18:06:03 +01:00
<input type="submit" style="display: none;">
2025-06-11 10:47:47 +02:00
<div class="error-label" v-if="formError.generic">{{ formError.generic }}</div>
<FormGroup :has-error="formError.password">
<label>{{ $t('profile.disable2FA.password') }}</label>
2025-12-05 18:06:03 +01:00
<PasswordInput v-model="password" required />
2025-06-11 10:47:47 +02:00
<div class="error-label" v-if="formError.password">{{ formError.password }}</div>
</FormGroup>
</fieldset>
</form>
</Dialog>
</template>