Skip to content

Commit

Permalink
fix(usersettings): allow unset email and add more explicit email erro…
Browse files Browse the repository at this point in the history
…r message (Fallenbagel#1096)
  • Loading branch information
gauthier-th authored and thibodelanghe committed Dec 18, 2024
1 parent 3d684d2 commit 9521bb6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
21 changes: 20 additions & 1 deletion server/routes/user/usersettings.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -138,7 +140,7 @@ const UserGeneralSettings = () => {
</div>
<Formik
initialValues={{
displayName: data?.username ?? '',
displayName: data?.username !== user?.email ? data?.username : '',
email: data?.email?.includes('@') ? data.email : '',
discordId: data?.discordId ?? '',
locale: data?.locale,
Expand Down Expand Up @@ -203,10 +205,23 @@ 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,
Expand Down Expand Up @@ -284,9 +299,9 @@ const UserGeneralSettings = () => {
name="displayName"
type="text"
placeholder={
user?.username ||
user?.jellyfinUsername ||
user?.plexUsername
user?.plexUsername ||
user?.email
}
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9521bb6

Please sign in to comment.