|
| 1 | +<template> |
| 2 | + <div class="biometric-login-form"> |
| 3 | + <v-row align="center" justify="center" no-gutters> |
| 4 | + <v-col cols="12" class="text-center"> |
| 5 | + <v-icon icon="mdi-fingerprint" size="64" :color="iconColor" class="mb-4" /> |
| 6 | + |
| 7 | + <h3 class="mb-4">{{ statusText }}</h3> |
| 8 | + |
| 9 | + <v-btn v-if="showRetryButton" class="a-btn-primary" @click="authenticate"> |
| 10 | + {{ t('biometric_login.try_again') }} |
| 11 | + </v-btn> |
| 12 | + |
| 13 | + <div class="text-center mt-6"> |
| 14 | + <h3 class="a-text-regular"> |
| 15 | + {{ t('biometric_login.use_passphrase_hint') }} |
| 16 | + </h3> |
| 17 | + <v-btn class="a-btn-link mt-2" variant="text" size="small" @click="switchToPassphrase"> |
| 18 | + {{ t('biometric_login.use_passphrase') }} |
| 19 | + </v-btn> |
| 20 | + </div> |
| 21 | + </v-col> |
| 22 | + </v-row> |
| 23 | + </div> |
| 24 | +</template> |
| 25 | + |
| 26 | +<script lang="ts" setup> |
| 27 | +import { computed, ref, onMounted } from 'vue' |
| 28 | +import { useI18n } from 'vue-i18n' |
| 29 | +import { biometricAuth } from '@/lib/auth' |
| 30 | +import { AuthenticationResult } from '@/lib/auth/types' |
| 31 | +import { useStore } from 'vuex' |
| 32 | +
|
| 33 | +const { t } = useI18n() |
| 34 | +const store = useStore() |
| 35 | +
|
| 36 | +const emit = defineEmits<{ |
| 37 | + (e: 'login'): void |
| 38 | + (e: 'error', error: string): void |
| 39 | +}>() |
| 40 | +
|
| 41 | +const isAuthenticating = ref(false) |
| 42 | +const showRetryButton = ref(false) |
| 43 | +const hasError = ref(false) |
| 44 | +
|
| 45 | +const iconColor = computed(() => { |
| 46 | + if (isAuthenticating.value) return 'primary' |
| 47 | + if (hasError.value) return 'error' |
| 48 | + return 'primary' |
| 49 | +}) |
| 50 | +
|
| 51 | +const statusText = computed(() => { |
| 52 | + if (isAuthenticating.value) return t('biometric_login.authenticating') |
| 53 | + if (hasError.value) return t('biometric_login.authentication_failed') |
| 54 | + return t('biometric_login.touch_sensor') |
| 55 | +}) |
| 56 | +
|
| 57 | +const handleAuthError = (errorMessage: string) => { |
| 58 | + showRetryButton.value = true |
| 59 | + hasError.value = true |
| 60 | + emit('error', errorMessage) |
| 61 | +} |
| 62 | +
|
| 63 | +const authenticate = async () => { |
| 64 | + isAuthenticating.value = true |
| 65 | + showRetryButton.value = false |
| 66 | + hasError.value = false |
| 67 | +
|
| 68 | + try { |
| 69 | + const biometricResult = await biometricAuth.authorizeUser() |
| 70 | +
|
| 71 | + if (biometricResult === AuthenticationResult.Cancel) { |
| 72 | + showRetryButton.value = true |
| 73 | + return |
| 74 | + } |
| 75 | +
|
| 76 | + if (biometricResult !== AuthenticationResult.Success) { |
| 77 | + handleAuthError(t('biometric_login.authentication_failed')) |
| 78 | + return |
| 79 | + } |
| 80 | +
|
| 81 | + await store.dispatch('loginViaAuthentication') |
| 82 | +
|
| 83 | + emit('login') |
| 84 | + } catch (error) { |
| 85 | + handleAuthError(error instanceof Error ? error.message : t('biometric_login.authentication_failed')) |
| 86 | + } finally { |
| 87 | + isAuthenticating.value = false |
| 88 | + } |
| 89 | +} |
| 90 | +
|
| 91 | +const switchToPassphrase = () => { |
| 92 | + store.commit('options/updateOption', { key: 'stayLoggedIn', value: false }) |
| 93 | + store.commit('options/updateOption', { key: 'authenticationMethod', value: null }) |
| 94 | +} |
| 95 | +
|
| 96 | +onMounted(() => { |
| 97 | + authenticate() |
| 98 | +}) |
| 99 | +</script> |
0 commit comments