diff --git a/web/app/components/FirebaseAuth.vue b/web/app/components/FirebaseAuth.vue index 1c8e076a..5f783e07 100644 --- a/web/app/components/FirebaseAuth.vue +++ b/web/app/components/FirebaseAuth.vue @@ -7,6 +7,8 @@ import { signInWithEmailAndPassword, createUserWithEmailAndPassword, sendPasswordResetEmail, + updateProfile, + deleteUser, } from 'firebase/auth' import { mdiGoogle, mdiGithub, mdiEmail } from '@mdi/js' import type { User as FirebaseUser } from 'firebase/auth' @@ -29,6 +31,7 @@ const showEmailForm = ref(false) const isSignUp = ref(false) const showForgotPassword = ref(false) const resetEmailSent = ref(false) +const name = ref('') const email = ref('') const password = ref('') const generalError = ref('') @@ -71,6 +74,10 @@ function validateEmail(): boolean { function validateLoginForm(): boolean { clearErrors() let valid = true + if (isSignUp.value && !name.value.trim()) { + errorMessages.value.add('name', 'Please provide your name') + valid = false + } if (!email.value.trim()) { errorMessages.value.add('email', 'Please provide an email address') valid = false @@ -126,6 +133,18 @@ async function submitEmail() { email.value.trim(), password.value, ) + try { + await updateProfile(result.user, { + displayName: name.value.trim(), + }) + } catch (error) { + try { + await deleteUser(result.user) + } catch (deleteError) { + console.error(deleteError) + } + throw error + } } else { result = await signInWithEmailAndPassword( auth, @@ -167,6 +186,11 @@ function backToSignIn() { showForgotPassword.value = false } +function toggleAuthMode() { + clearErrors() + isSignUp.value = !isSignUp.value +} + function onSuccess(user: FirebaseUser, method: LoginMethod) { try { localStorage.setItem(LAST_LOGIN_METHOD_KEY, method) @@ -405,11 +429,27 @@ function getGeneralErrorMessage( class="mt-4" @submit.prevent="submitEmail" > + {{ isSignUp ? 'Already have an account? Sign In' : 'No account? Sign Up'