Skip to content

Commit

Permalink
created a function to delete account using user's ID token
Browse files Browse the repository at this point in the history
  • Loading branch information
benlee04 committed Nov 8, 2024
1 parent 46fd440 commit c44dc2d
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,37 @@ const AccountAction = () => {
"Are you sure you want to delete your account? This action cannot be undone.",
[
{ text: "Cancel", style: "cancel" },
{ text: "Delete", style: "destructive"},
{ text: "Delete", style: "destructive", onPress: handleDeleteAccount },
]
);
};

const handleDeleteAccount = async () => {
if (!user) {
console.log("No user logged in");
return;
}

try {
// API call to delete account
const idToken = await user.getIdToken(); // Get user ID token for authorization
await api.delete(`/auth/delete-account/${user.uid}`, {
headers: { Authorization: `Bearer ${idToken}` },
});

// Sign the user out
await auth.signOut();

// Clear user context and navigate to SignIn screen
setUser(null);
Alert.alert("Account Deleted", "Your account has been successfully deleted.");

} catch (error) {
console.error("Error deleting account:", error);
Alert.alert("Error", "Failed to delete account. Please try again.");
}
};

return (
<View>
<Text className="mb-1 font-[600]">ACCOUNT ACTION</Text>
Expand Down

0 comments on commit c44dc2d

Please sign in to comment.