From 028daff0648d7758984b04960f24e8bf20002ea4 Mon Sep 17 00:00:00 2001 From: linus345 <46067241+linus345@users.noreply.github.com> Date: Wed, 8 Jul 2020 04:03:35 +0200 Subject: [PATCH] fix(login): improved login validation Co-authored-by: Jack Meyer --- src/__tests__/user/user-slice.test.ts | 4 +-- src/login/Login.tsx | 12 ++++++++- src/shared/locales/enUs/translations/index.ts | 2 ++ .../locales/enUs/translations/user/index.ts | 18 +++++++++++++ src/user/user-slice.ts | 26 ++++++++++++++++--- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/shared/locales/enUs/translations/user/index.ts diff --git a/src/__tests__/user/user-slice.test.ts b/src/__tests__/user/user-slice.test.ts index 4c388ef92d..8515690f82 100644 --- a/src/__tests__/user/user-slice.test.ts +++ b/src/__tests__/user/user-slice.test.ts @@ -98,7 +98,7 @@ describe('user slice', () => { }) it('should dispatch login error if login was not successful', async () => { - jest.spyOn(remoteDb, 'logIn').mockRejectedValue({ status: '401' }) + jest.spyOn(remoteDb, 'logIn').mockRejectedValue({ status: 401 }) jest.spyOn(remoteDb, 'getUser').mockResolvedValue({ _id: 'userId', metadata: { @@ -113,7 +113,7 @@ describe('user slice', () => { expect(remoteDb.getUser).not.toHaveBeenCalled() expect(store.getActions()[0]).toEqual({ type: loginError.type, - payload: 'user.login.error', + payload: { message: 'user.login.error.message.incorrect' }, }) }) }) diff --git a/src/login/Login.tsx b/src/login/Login.tsx index a7cf37aacc..a6125f0548 100644 --- a/src/login/Login.tsx +++ b/src/login/Login.tsx @@ -6,12 +6,14 @@ import { Redirect } from 'react-router-dom' import TextInputWithLabelFormGroup from '../shared/components/input/TextInputWithLabelFormGroup' import { remoteDb } from '../shared/config/pouchdb' +import useTranslator from '../shared/hooks/useTranslator' import logo from '../shared/static/images/logo-on-transparent.png' import { RootState } from '../shared/store' import { getCurrentSession, login } from '../user/user-slice' const Login = () => { const dispatch = useDispatch() + const { t } = useTranslator() const [username, setUsername] = useState('') const [password, setPassword] = useState('') const { loginError, user } = useSelector((root: RootState) => root.user) @@ -61,13 +63,18 @@ const Login = () => { HospitalRun
- {loginError && } + {loginError?.message && ( + + )} { name="password" value={password} onChange={onPasswordChange} + isRequired + isInvalid={!!loginError?.password && !password} + feedback={t(loginError?.password)} />