From e5651c152a9976a3dbb21d53864e2373c33b2c43 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Thu, 15 May 2025 22:32:16 +0800 Subject: [PATCH] Update Profile Form for Changing email requires verification. A verification email will be sent. --- .../components/profile/UpdateProfileForm.tsx | 68 ++++++++++++++++--- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/application/src/components/profile/UpdateProfileForm.tsx b/application/src/components/profile/UpdateProfileForm.tsx index 8911530..b335e2d 100644 --- a/application/src/components/profile/UpdateProfileForm.tsx +++ b/application/src/components/profile/UpdateProfileForm.tsx @@ -9,6 +9,8 @@ import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from " import { Input } from "@/components/ui/input"; import { useToast } from "@/hooks/use-toast"; import { authService } from "@/services/authService"; +import { AlertCircle } from "lucide-react"; +import { Alert, AlertDescription } from "@/components/ui/alert"; // Profile update form schema const profileFormSchema = z.object({ @@ -31,6 +33,8 @@ interface UpdateProfileFormProps { export function UpdateProfileForm({ user }: UpdateProfileFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); + const [emailChangeRequested, setEmailChangeRequested] = useState(false); + const [updateError, setUpdateError] = useState(null); const { toast } = useToast(); // Initialize the form with current user data @@ -45,21 +49,45 @@ export function UpdateProfileForm({ user }: UpdateProfileFormProps) { async function onSubmit(data: ProfileFormValues) { setIsSubmitting(true); + setUpdateError(null); + setEmailChangeRequested(false); try { - await userService.updateUser(user.id, { + console.log("Submitting profile update with data:", data); + + // Detect if email is being changed + const isEmailChanged = data.email !== user.email; + + // Create update payload with all fields + const updateData = { full_name: data.full_name, username: data.username, - email: data.email, - }); + email: isEmailChanged ? data.email : undefined, + // Only set emailVisibility if email is being changed + emailVisibility: isEmailChanged ? true : undefined + }; + + console.log("Sending update payload:", updateData); + + // Update user data using the userService + await userService.updateUser(user.id, updateData); // Refresh user data in auth context await authService.refreshUserData(); - toast({ - title: "Profile updated", - description: "Your profile information has been updated successfully.", - }); + // If email was changed, show a specific message + if (isEmailChanged) { + setEmailChangeRequested(true); + toast({ + title: "Email change requested", + description: "A verification email has been sent to your new email address. Please check your inbox and follow the instructions to complete the change.", + }); + } else { + toast({ + title: "Profile updated", + description: "Your profile information has been updated successfully.", + }); + } } catch (error) { console.error("Profile update error:", error); @@ -68,6 +96,8 @@ export function UpdateProfileForm({ user }: UpdateProfileFormProps) { errorMessage = error.message; } + setUpdateError(errorMessage); + toast({ title: "Update failed", description: errorMessage, @@ -81,6 +111,23 @@ export function UpdateProfileForm({ user }: UpdateProfileFormProps) { return (
+ {updateError && ( + + + {updateError} + + )} + + {emailChangeRequested && ( + + + + A verification email has been sent to your new email address. + Please check your inbox and follow the instructions to complete the change. + + + )} + + {field.value !== user.email && ( +

+ Changing your email requires verification. A verification email will be sent. +

+ )} )} /> @@ -129,4 +181,4 @@ export function UpdateProfileForm({ user }: UpdateProfileFormProps) { ); -} +} \ No newline at end of file