diff --git a/server/routes/user/usersettings.ts b/server/routes/user/usersettings.ts index 11cbd666f..62fd05aef 100644 --- a/server/routes/user/usersettings.ts +++ b/server/routes/user/usersettings.ts @@ -1,4 +1,5 @@ import { ApiErrorCode } from '@server/constants/error'; +import { UserType } from '@server/constants/user'; import { getRepository } from '@server/datasource'; import { User } from '@server/entity/User'; import { UserSettings } from '@server/entity/UserSettings'; @@ -99,11 +100,29 @@ userSettingsRoutes.post< }); } - user.username = req.body.username; const oldEmail = user.email; + const oldUsername = user.username; + user.username = req.body.username; if (user.jellyfinUsername) { user.email = req.body.email || user.jellyfinUsername || user.email; } + // Edge case for local users, because they have no Jellyfin username to fall back on + // if the email is not provided + if (user.userType === UserType.LOCAL) { + if (req.body.email) { + user.email = req.body.email; + if ( + !user.username && + user.email !== oldEmail && + !oldEmail.includes('@') + ) { + user.username = oldEmail; + } + } else if (req.body.username) { + user.email = oldUsername || user.email; + user.username = req.body.username; + } + } const existingUser = await userRepository.findOne({ where: { email: user.email }, diff --git a/src/components/UserProfile/UserSettings/UserGeneralSettings/index.tsx b/src/components/UserProfile/UserSettings/UserGeneralSettings/index.tsx index 502a9d84f..961b9b1e0 100644 --- a/src/components/UserProfile/UserSettings/UserGeneralSettings/index.tsx +++ b/src/components/UserProfile/UserSettings/UserGeneralSettings/index.tsx @@ -44,6 +44,8 @@ const messages = defineMessages( toastSettingsSuccess: 'Settings saved successfully!', toastSettingsFailure: 'Something went wrong while saving settings.', toastSettingsFailureEmail: 'This email is already taken!', + toastSettingsFailureEmailEmpty: + 'Another user already has this username. You must set an email', region: 'Discover Region', regionTip: 'Filter content by regional availability', originallanguage: 'Discover Language', @@ -138,7 +140,7 @@ const UserGeneralSettings = () => { { /* empty */ } if (errorData?.message === ApiErrorCode.InvalidEmail) { - addToast(intl.formatMessage(messages.toastSettingsFailureEmail), { - autoDismiss: true, - appearance: 'error', - }); + if (values.email) { + addToast( + intl.formatMessage(messages.toastSettingsFailureEmail), + { + autoDismiss: true, + appearance: 'error', + } + ); + } else { + addToast( + intl.formatMessage(messages.toastSettingsFailureEmailEmpty), + { + autoDismiss: true, + appearance: 'error', + } + ); + } } else { addToast(intl.formatMessage(messages.toastSettingsFailure), { autoDismiss: true, @@ -284,9 +299,9 @@ const UserGeneralSettings = () => { name="displayName" type="text" placeholder={ - user?.username || user?.jellyfinUsername || - user?.plexUsername + user?.plexUsername || + user?.email } /> diff --git a/src/i18n/locale/en.json b/src/i18n/locale/en.json index c830911b6..7086acfc3 100644 --- a/src/i18n/locale/en.json +++ b/src/i18n/locale/en.json @@ -1248,6 +1248,7 @@ "components.UserProfile.UserSettings.UserGeneralSettings.seriesrequestlimit": "Series Request Limit", "components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsFailure": "Something went wrong while saving settings.", "components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsFailureEmail": "This email is already taken!", + "components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsFailureEmailEmpty": "Another user already has this username. You must set an email", "components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsSuccess": "Settings saved successfully!", "components.UserProfile.UserSettings.UserGeneralSettings.user": "User", "components.UserProfile.UserSettings.UserGeneralSettings.validationDiscordId": "You must provide a valid Discord user ID",