Skip to content

Commit

Permalink
fix: better error message when creating a user with an existing email
Browse files Browse the repository at this point in the history
fixes #1441
  • Loading branch information
sct committed Apr 18, 2021
1 parent eb5d152 commit f13f1c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 12 additions & 0 deletions server/routes/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ router.post(
const body = req.body;
const userRepository = getRepository(User);

const existingUser = await userRepository.findOne({
where: { email: body.email },
});

if (existingUser) {
return next({
status: 409,
message: 'User already exists with submitted email.',
errors: ['USER_EXISTS'],
});
}

const passedExplicitPassword = body.password && body.password.length > 0;
const avatar = gravatarUrl(body.email, { default: 'mm', size: 200 });

Expand Down
17 changes: 13 additions & 4 deletions src/components/UserList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const messages = defineMessages({
validationpasswordminchars:
'Password is too short; should be a minimum of 8 characters',
usercreatedfailed: 'Something went wrong while creating the user.',
usercreatedfailedexisting:
'Provided email is already in use by another user.',
usercreatedsuccess: 'User created successfully!',
email: 'Email Address',
password: 'Password',
Expand Down Expand Up @@ -305,10 +307,17 @@ const UserList: React.FC = () => {
});
setCreateModal({ isOpen: false });
} catch (e) {
addToast(intl.formatMessage(messages.usercreatedfailed), {
appearance: 'error',
autoDismiss: true,
});
addToast(
intl.formatMessage(
e.response.data.errors?.includes('USER_EXISTS')
? messages.usercreatedfailedexisting
: messages.usercreatedfailed
),
{
appearance: 'error',
autoDismiss: true,
}
);
} finally {
revalidate();
}
Expand Down

0 comments on commit f13f1c9

Please sign in to comment.