From f13f1c94515b5bd51382fa18ad96a2ccfd06e50d Mon Sep 17 00:00:00 2001 From: sct Date: Sun, 18 Apr 2021 19:53:55 +0900 Subject: [PATCH] fix: better error message when creating a user with an existing email fixes #1441 --- server/routes/user/index.ts | 12 ++++++++++++ src/components/UserList/index.tsx | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/server/routes/user/index.ts b/server/routes/user/index.ts index 6546d666f..a0dab71c8 100644 --- a/server/routes/user/index.ts +++ b/server/routes/user/index.ts @@ -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 }); diff --git a/src/components/UserList/index.tsx b/src/components/UserList/index.tsx index 63f5bf5fd..95516d488 100644 --- a/src/components/UserList/index.tsx +++ b/src/components/UserList/index.tsx @@ -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', @@ -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(); }