Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(settings): surface backend error when email change is rejected (#61880)

Christiaan Hendriksena115d10a82b7
diff --git a/frontend/src/scenes/settings/user/UserDetails.tsx b/frontend/src/scenes/settings/user/UserDetails.tsx
index 0caab0e209d6..d2f4751a3343 100644
--- a/frontend/src/scenes/settings/user/UserDetails.tsx
+++ b/frontend/src/scenes/settings/user/UserDetails.tsx
@@ -10,6 +10,8 @@ export function UserDetails(): JSX.Element {
const { userLoading, isUserDetailsSubmitting, userDetailsChanged, user } = useValues(userLogic)
const { cancelEmailChangeRequest } = useActions(userLogic)
+ const ssoEnforced = !!user?.has_sso_enforcement
+
return (
<Form
logic={userLogic}
@@ -43,9 +45,14 @@ export function UserDetails(): JSX.Element {
className="ph-ignore-input"
data-attr="settings-update-email"
placeholder="[email protected]"
- disabled={userLoading}
+ disabled={userLoading || ssoEnforced}
/>
</LemonField>
+ {ssoEnforced && (
+ <div className="text-secondary text-xs">
+ You can't change your email because your organization enforces SSO on your email's domain.
+ </div>
+ )}
{user?.pending_email && (
<div className="flex flex-row gap-2">
<div className="text-danger text-xs font-medium mt-1.25">
diff --git a/frontend/src/scenes/userLogic.test.ts b/frontend/src/scenes/userLogic.test.ts
index aff13f342d8f..1c8c63ae2731 100644
--- a/frontend/src/scenes/userLogic.test.ts
+++ b/frontend/src/scenes/userLogic.test.ts
@@ -186,4 +186,33 @@ describe('userLogic', () => {
})
})
})
+
+ describe('updateUser failure handling', () => {
+ afterEach(() => {
+ jest.restoreAllMocks()
+ })
+
+ it('dispatches updateUserFailure (not success) when the backend rejects the update', async () => {
+ jest.spyOn(api, 'update').mockRejectedValue({
+ status: 400,
+ detail: 'Update rejected by server.',
+ })
+
+ await expectLogic(userLogic, () => {
+ userLogic.actions.updateUser({ email: '[email protected]' })
+ })
+ .toDispatchActions(['updateUserFailure'])
+ .toNotHaveDispatchedActions(['updateUserSuccess'])
+ })
+
+ it('keeps the existing user when the update fails — no silent revert via success', async () => {
+ jest.spyOn(api, 'update').mockRejectedValue({ status: 400, detail: 'nope' })
+
+ await expectLogic(userLogic, () => {
+ userLogic.actions.updateUser({ email: '[email protected]' })
+ })
+ .toDispatchActions(['updateUserFailure'])
+ .toMatchValues({ user: userWithLightTheme })
+ })
+ })
})
diff --git a/frontend/src/scenes/userLogic.ts b/frontend/src/scenes/userLogic.ts
index afca872d146c..d53dda4789d5 100644
--- a/frontend/src/scenes/userLogic.ts
+++ b/frontend/src/scenes/userLogic.ts
@@ -150,15 +150,11 @@ export const userLogic = kea<userLogicType>([
if (!values.user) {
throw new Error('Current user has not been loaded yet, so it cannot be updated!')
}
- try {
- const response = await api.update<UserType>('api/users/@me/', user)
- successCallback?.()
- return response
- } catch (error: any) {
- console.error(error)
- actions.updateUserFailure(error.message)
- return values.user
- }
+ // Let failures throw so kea-loaders dispatches `updateUserFailure` — returning the old
+ // user here would be treated as a success, silently masking backend errors.
+ const response = await api.update<UserType>('api/users/@me/', user)
+ successCallback?.()
+ return response
},
cancelEmailChangeRequest: async () => {
if (!values.user) {
@@ -351,8 +347,9 @@ export const userLogic = kea<userLogicType>([
toastId: 'updateUser',
})
},
- updateUserFailure: () => {
- lemonToast.error(`Error saving preferences`, {
+ updateUserFailure: ({ errorObject }) => {
+ lemonToast.dismiss('updateUser')
+ lemonToast.error(errorObject?.detail || 'Error saving preferences', {
toastId: 'updateUser',
})
},