diff --git a/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx b/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx index 27182e2b5c..672054fe6a 100644 --- a/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx +++ b/apps/web/components/ui/UsernameAvailability/PremiumTextfield.tsx @@ -96,7 +96,7 @@ const PremiumTextfield = (props: ICustomUsernameProps) => { const updateUsername = trpc.viewer.updateProfile.useMutation({ onSuccess: async () => { onSuccessMutation && (await onSuccessMutation()); - await update(); + await update({ username: inputUsernameValue }); setOpenDialogSaveUsername(false); }, onError: (error) => { diff --git a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx index 5c04ffc26d..ee3f583d9e 100644 --- a/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx +++ b/apps/web/components/ui/UsernameAvailability/UsernameTextfield.tsx @@ -25,6 +25,7 @@ interface ICustomUsernameProps { const UsernameTextfield = (props: ICustomUsernameProps & Partial>) => { const { t } = useLocale(); const { data: session, update } = useSession(); + const { currentUsername, setCurrentUsername = noop, @@ -71,7 +72,7 @@ const UsernameTextfield = (props: ICustomUsernameProps & Partial { onErrorMutation && onErrorMutation(error); diff --git a/apps/web/pages/settings/my-account/profile.tsx b/apps/web/pages/settings/my-account/profile.tsx index 584fb8913b..9ffa5d7c25 100644 --- a/apps/web/pages/settings/my-account/profile.tsx +++ b/apps/web/pages/settings/my-account/profile.tsx @@ -1,5 +1,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { signOut } from "next-auth/react"; +import { useSession } from "next-auth/react"; import type { BaseSyntheticEvent } from "react"; import { useRef, useState } from "react"; import { Controller, useForm } from "react-hook-form"; @@ -78,13 +79,16 @@ type FormValues = { const ProfileView = () => { const { t } = useLocale(); const utils = trpc.useContext(); + const { data: session, update } = useSession(); + const { data: user, isLoading } = trpc.viewer.me.useQuery(); const { data: avatar, isLoading: isLoadingAvatar } = trpc.viewer.avatar.useQuery(); const mutation = trpc.viewer.updateProfile.useMutation({ - onSuccess: () => { + onSuccess: (values) => { showToast(t("settings_updated_successfully"), "success"); utils.viewer.me.invalidate(); utils.viewer.avatar.invalidate(); + update(values); setTempFormValues(null); }, onError: () => { @@ -115,7 +119,9 @@ const ProfileView = () => { const confirmPasswordMutation = trpc.viewer.auth.verifyPassword.useMutation({ onSuccess() { - if (tempFormValues) mutation.mutate(tempFormValues); + if (tempFormValues) { + mutation.mutate(tempFormValues); + } setConfirmPasswordOpen(false); }, onError() { diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts index 6a1033e5bc..e9f537988a 100644 --- a/packages/features/auth/lib/next-auth-options.ts +++ b/packages/features/auth/lib/next-auth-options.ts @@ -350,6 +350,14 @@ export const AUTH_OPTIONS: AuthOptions = { providers, callbacks: { async jwt({ token, user, account, trigger, session }) { + if (trigger === "update") { + return { + ...token, + name: session?.name ?? token.name, + username: session?.username ?? token.username, + email: session?.email ?? token.email, + }; + } const autoMergeIdentities = async () => { const existingUser = await prisma.user.findFirst({ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts b/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts index 0207f44c42..089fd3ad37 100644 --- a/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts +++ b/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts @@ -98,6 +98,11 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions) }); } } + const hasEmailBeenChanged = userToUpdate.email !== data.email; + + if (hasEmailBeenChanged) { + data.emailVerified = null; + } const updatedUser = await prisma.user.update({ where: {