From e02cf035c90c07492a0ba3f33a5f91dfb266626a Mon Sep 17 00:00:00 2001 From: Sana Yasmin Date: Wed, 1 Jan 2025 22:39:19 +0530 Subject: [PATCH] feat:#684 - Delete account --- src/components/Profile/SettingsTab.vue | 44 ++++++++++++++++++++++++++ src/stores/user.js | 44 ++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/components/Profile/SettingsTab.vue b/src/components/Profile/SettingsTab.vue index ee2a2ff9..69b7de10 100644 --- a/src/components/Profile/SettingsTab.vue +++ b/src/components/Profile/SettingsTab.vue @@ -1,15 +1,44 @@ diff --git a/src/stores/user.js b/src/stores/user.js index 3946c6a1..007d0a54 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -4,7 +4,8 @@ import { GoogleAuthProvider, signInWithEmailAndPassword, signInWithPopup, - signOut + signOut, + getAuth } from 'firebase/auth' import { collection, doc, getDoc, getDocs, onSnapshot, or, query, runTransaction, setDoc, where } from 'firebase/firestore' import { defineStore } from 'pinia' @@ -245,7 +246,7 @@ export const useUserStore = defineStore('user', { } finally { this._isLoading = false } - } + }, // async addAllUsers(users) { // await fetch(`${baseURL}/add-all-users`, { @@ -263,5 +264,44 @@ export const useUserStore = defineStore('user', { // }) // this._statsUsers = await allUsers.json() // } + + async deleteOwnAccount() { + this._isLoading = true + try { + const currentUser = getAuth().currentUser + if (!currentUser) { + throw new Error('User is not authenticated.') + } + + const userDocRef = doc(db, 'users', currentUser.uid) + await deleteDoc(userDocRef) + + await currentUser.delete() + + Notify.create({ + color: 'positive', + message: 'Your account has been deleted successfully.' + }) + + this.$reset() + LocalStorage.remove('user') + } catch (error) { + console.error('Error deleting account:', error) + + if (error.code === 'auth/requires-recent-login') { + Notify.create({ + color: 'negative', + message: 'Please log in again to delete your account.' + }) + } else { + Notify.create({ + color: 'negative', + message: 'Failed to delete your account. Please try again later.' + }) + } + } finally { + this._isLoading = false + } + } } })