🐛 Fix issues with email change flow (#1495)

This commit is contained in:
Luke Vella 2025-01-14 18:03:33 +00:00 committed by GitHub
parent ff3d992f71
commit 4e2661ba01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 14 deletions

View file

@ -289,5 +289,5 @@
"emailChangeRequestSentDescription": "To complete the change, please check your email for a verification link.", "emailChangeRequestSentDescription": "To complete the change, please check your email for a verification link.",
"profileEmailAddress": "Email Address", "profileEmailAddress": "Email Address",
"profileEmailAddressDescription": "Your email address is used to log in to your account", "profileEmailAddressDescription": "Your email address is used to log in to your account",
"emailAlreadyInUse": "Email already in use. Please try a different one or delete the existing account." "emailAlreadyInUse": "This email address is already associated with another account. Please use a different email address."
} }

View file

@ -1,3 +1,4 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { usePostHog } from "@rallly/posthog/client"; import { usePostHog } from "@rallly/posthog/client";
import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert"; import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
import { Button } from "@rallly/ui/button"; import { Button } from "@rallly/ui/button";
@ -16,23 +17,26 @@ import { InfoIcon } from "lucide-react";
import React from "react"; import React from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { z } from "zod";
import { Trans } from "@/components/trans"; import { Trans } from "@/components/trans";
import { useUser } from "@/components/user-provider"; import { useUser } from "@/components/user-provider";
import { trpc } from "@/trpc/client"; import { trpc } from "@/trpc/client";
const emailChangeFormData = z.object({
email: z.string().email(),
});
type EmailChangeFormData = z.infer<typeof emailChangeFormData>;
export const ProfileEmailAddress = () => { export const ProfileEmailAddress = () => {
const { user, refresh } = useUser(); const { user, refresh } = useUser();
const requestEmailChange = trpc.user.requestEmailChange.useMutation(); const requestEmailChange = trpc.user.requestEmailChange.useMutation();
const posthog = usePostHog(); const posthog = usePostHog();
const form = useForm<{ const form = useForm<EmailChangeFormData>({
name: string;
email: string;
}>({
defaultValues: { defaultValues: {
name: user.isGuest ? "" : user.name,
email: user.email ?? "", email: user.email ?? "",
}, },
resolver: zodResolver(emailChangeFormData),
}); });
const { t } = useTranslation("app"); const { t } = useTranslation("app");
const { toast } = useToast(); const { toast } = useToast();
@ -82,7 +86,6 @@ export const ProfileEmailAddress = () => {
<Form {...form}> <Form {...form}>
<form <form
onSubmit={handleSubmit(async (data) => { onSubmit={handleSubmit(async (data) => {
reset(data);
if (data.email !== user.email) { if (data.email !== user.email) {
posthog.capture("email change requested"); posthog.capture("email change requested");
const res = await requestEmailChange.mutateAsync({ const res = await requestEmailChange.mutateAsync({
@ -99,9 +102,9 @@ export const ProfileEmailAddress = () => {
} }
} else { } else {
setDidRequestEmailChange(true); setDidRequestEmailChange(true);
reset(data);
} }
} }
await refresh();
})} })}
> >
<div className="flex flex-col gap-y-4"> <div className="flex flex-col gap-y-4">

View file

@ -1,3 +1,4 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { Button } from "@rallly/ui/button"; import { Button } from "@rallly/ui/button";
import { import {
Form, Form,
@ -5,27 +6,32 @@ import {
FormField, FormField,
FormItem, FormItem,
FormLabel, FormLabel,
FormMessage,
} from "@rallly/ui/form"; } from "@rallly/ui/form";
import { Input } from "@rallly/ui/input"; import { Input } from "@rallly/ui/input";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { z } from "zod";
import { ProfilePicture } from "@/app/[locale]/(admin)/settings/profile/profile-picture"; import { ProfilePicture } from "@/app/[locale]/(admin)/settings/profile/profile-picture";
import { Trans } from "@/components/trans"; import { Trans } from "@/components/trans";
import { useUser } from "@/components/user-provider"; import { useUser } from "@/components/user-provider";
import { trpc } from "@/trpc/client"; import { trpc } from "@/trpc/client";
const profileSettingsFormData = z.object({
name: z.string().min(1).max(100),
});
type ProfileSettingsFormData = z.infer<typeof profileSettingsFormData>;
export const ProfileSettings = () => { export const ProfileSettings = () => {
const { user, refresh } = useUser(); const { user, refresh } = useUser();
const changeName = trpc.user.changeName.useMutation(); const changeName = trpc.user.changeName.useMutation();
const form = useForm<{ const form = useForm<ProfileSettingsFormData>({
name: string;
email: string;
}>({
defaultValues: { defaultValues: {
name: user.isGuest ? "" : user.name, name: user.isGuest ? "" : user.name,
email: user.email ?? "",
}, },
resolver: zodResolver(profileSettingsFormData),
}); });
const { control, handleSubmit, formState, reset } = form; const { control, handleSubmit, formState, reset } = form;
@ -54,6 +60,7 @@ export const ProfileSettings = () => {
<FormControl> <FormControl>
<Input id="name" {...field} /> <Input id="name" {...field} />
</FormControl> </FormControl>
<FormMessage />
</FormItem> </FormItem>
)} )}
/> />

View file

@ -52,6 +52,6 @@
"changeEmailRequest_text2": "To complete this change, please click the button below:", "changeEmailRequest_text2": "To complete this change, please click the button below:",
"changeEmailRequest_button": "Verify Email Address", "changeEmailRequest_button": "Verify Email Address",
"changeEmailRequest_subject": "Verify your new email address", "changeEmailRequest_subject": "Verify your new email address",
"changeEmailRequest_text3": "This link will expire in 10 minutes. If you did not request this change, please contact support.", "changeEmailRequest_text3": "This link will expire in 10 minutes. If you did not request this change, please ignore this email.",
"changeEmailRequest_text1": "We've received a request to change the email address for your account from <b>{{fromEmail}}</b> to <b>{{toEmail}}</b>." "changeEmailRequest_text1": "We've received a request to change the email address for your account from <b>{{fromEmail}}</b> to <b>{{toEmail}}</b>."
} }