-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/password reset and change (#146)
* feat: password reset * feat: password reset and password change
- Loading branch information
1 parent
e33fb29
commit 20fdc11
Showing
15 changed files
with
476 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { FiUser } from 'react-icons/fi'; | ||
import { useAuth } from '../../hooks/auth'; | ||
import LoggedUserModal from '../LoggedUserModal'; | ||
|
||
import { Container } from './styles'; | ||
|
||
const LoggedUser: React.FC = () => { | ||
const { user } = useAuth(); | ||
const [loggedUserModal, setLoggedUserModal] = useState(false); | ||
|
||
const handleloggedUserModal = useCallback(() => { | ||
setLoggedUserModal((oldState) => !oldState); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Container onClick={handleloggedUserModal}> | ||
<FiUser size={16} /> | ||
<span>{user && user.name}</span> | ||
</Container> | ||
<LoggedUserModal | ||
isOpen={loggedUserModal} | ||
setOpen={handleloggedUserModal} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default LoggedUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { tint } from 'polished'; | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.div` | ||
display: flex; | ||
padding: 6px; | ||
height: 100%; | ||
cursor: pointer; | ||
&:hover { | ||
background-color: ${(props) => tint(0.4 ,props.theme.colors.background)}; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { FiLogOut, FiRefreshCw } from 'react-icons/fi'; | ||
import ReactModal from 'react-modal'; | ||
import { useToast } from '../../hooks/toast'; | ||
import { useAuth } from '../../hooks/auth'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import i18n from '../../i18n'; | ||
import Button from '../Button'; | ||
import Input from '../Input'; | ||
import { Container, Row } from './styles'; | ||
|
||
interface ModalProps { | ||
isOpen: boolean; | ||
setOpen: () => void; | ||
} | ||
|
||
const LoggedUserModal: React.FC<ModalProps> = ({ isOpen, setOpen }) => { | ||
const { user, signOut } = useAuth(); | ||
const { addToast } = useToast(); | ||
const navigate = useNavigate(); | ||
|
||
const [oldPassword, setOldPassword] = useState(''); | ||
const [newPassword, setNewPassword] = useState(''); | ||
const [confirmPassword, setConfirmPassword] = useState(''); | ||
|
||
const handleUpdatePassword = useCallback(() => { | ||
if (!oldPassword) { | ||
addToast({ | ||
title: i18n.t('notifications.warning'), | ||
type: 'error', | ||
description: i18n.t('profile.typePassword'), | ||
}); | ||
return; | ||
} | ||
if (!newPassword) { | ||
addToast({ | ||
title: i18n.t('notifications.warning'), | ||
type: 'error', | ||
description: i18n.t('profile.typeNewPassword'), | ||
}); | ||
return; | ||
} | ||
|
||
if (!confirmPassword) { | ||
addToast({ | ||
title: i18n.t('notifications.warning'), | ||
type: 'error', | ||
description: i18n.t('profile.typeConfirmPassword'), | ||
}); | ||
return; | ||
} | ||
|
||
if (newPassword !== confirmPassword) { | ||
addToast({ | ||
title: i18n.t('notifications.warning'), | ||
type: 'error', | ||
description: i18n.t('profile.newAndConfirmPassword'), | ||
}); | ||
return; | ||
} | ||
|
||
const changed = window.api.sendSync('changePassword', { | ||
entity: 'User', | ||
value: { | ||
userId: user.id, | ||
password: oldPassword, | ||
newPassword, | ||
}, | ||
}) as { id: string }; | ||
|
||
if (!changed) { | ||
addToast({ | ||
title: i18n.t('notifications.warning'), | ||
type: 'error', | ||
description: i18n.t('profile.oldPasswordInvalid'), | ||
}); | ||
return; | ||
} | ||
|
||
addToast({ | ||
title: i18n.t('notifications.success'), | ||
type: 'success', | ||
description: i18n.t('profile.successPasswordChange'), | ||
}); | ||
|
||
setOldPassword(''); | ||
setNewPassword(''); | ||
setConfirmPassword(''); | ||
}, [addToast, confirmPassword, newPassword, oldPassword, user.id]); | ||
|
||
return ( | ||
<ReactModal | ||
shouldCloseOnOverlayClick={true} | ||
onRequestClose={setOpen} | ||
isOpen={isOpen} | ||
ariaHideApp={false} | ||
className={'password-modal'} | ||
overlayClassName="modal-overlay" | ||
> | ||
<Container> | ||
<h2>{user.name}</h2> | ||
<Row> | ||
<h3>{i18n.t('button.changePassword')}</h3> | ||
</Row> | ||
<Row> | ||
<Input | ||
type="password" | ||
name="oldPassword" | ||
placeholder={i18n.t('person.password')} | ||
value={oldPassword} | ||
onChange={(e) => setOldPassword(e.target.value)} | ||
/> | ||
</Row> | ||
<Row> | ||
<Input | ||
type="password" | ||
name="newPassword" | ||
placeholder={i18n.t('person.newPassword')} | ||
value={newPassword} | ||
onChange={(e) => setNewPassword(e.target.value)} | ||
/> | ||
</Row> | ||
<Row> | ||
<Input | ||
type="password" | ||
name="confirmPassword" | ||
placeholder={i18n.t('person.confirmPassword')} | ||
value={confirmPassword} | ||
onChange={(e) => setConfirmPassword(e.target.value)} | ||
/> | ||
</Row> | ||
<Row> | ||
<Button | ||
color="primary" | ||
title={i18n.t('button.changePassword')} | ||
onClick={handleUpdatePassword} | ||
> | ||
<FiRefreshCw size={20} /> | ||
| ||
{i18n.t('button.changePassword')} | ||
</Button> | ||
</Row> | ||
|
||
<Row> | ||
<Button | ||
color="primary" | ||
title={i18n.t('button.logout')} | ||
onClick={signOut} | ||
> | ||
<FiLogOut size={20} /> | ||
| ||
{i18n.t('button.logout')} | ||
</Button> | ||
</Row> | ||
</Container> | ||
</ReactModal> | ||
); | ||
}; | ||
|
||
export default LoggedUserModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 24px; | ||
`; | ||
|
||
export const Row = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 16px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.