-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
305 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, { useCallback, useMemo, useState } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import PermissionDialog from '../PermissionDialog' | ||
import Buttons from '../../Buttons' | ||
import { useI18n } from '../../I18n' | ||
import Typography from '../../Typography' | ||
import { useClient } from 'cozy-client' | ||
import CozyAuthentificationIcon from '../../Icons/CozyAuthentification' | ||
|
||
import PasswordField from './PasswordField' | ||
import withSpecificDialogLocales from './locales/withSpecificDialogLocales' | ||
|
||
/** | ||
* Dialog used to authenticate a user in the cozy system. | ||
* The authentication logic is implemented in the applications. | ||
*/ | ||
const AuthentificationDialog = ({ | ||
closable, | ||
onDismiss, | ||
onAuthentification, | ||
isLoading, | ||
isOIDC, | ||
error | ||
}) => { | ||
const { t } = useI18n() | ||
const client = useClient() | ||
const [password, setPassword] = useState('') | ||
|
||
const handleAuthentification = useCallback( | ||
e => { | ||
e.preventDefault() | ||
onAuthentification(password) | ||
}, | ||
[onAuthentification, password] | ||
) | ||
|
||
const onPasswordChange = e => { | ||
setPassword(e.currentTarget.value) | ||
} | ||
|
||
const getPassphraseResetUrlCb = useMemo(() => { | ||
const url = new URL('/auth/passphrase_reset', client.getStackClient().uri) | ||
return url.href | ||
}, [client]) | ||
|
||
return ( | ||
<PermissionDialog | ||
size="small" | ||
open | ||
onClose={closable && onDismiss} | ||
title={t(`authentification.${isOIDC ? 'title-oidc' : 'title'}`)} | ||
icon={CozyAuthentificationIcon} | ||
content={ | ||
<form onSubmit={handleAuthentification}> | ||
<Typography variant="body1" className="u-ta-center"> | ||
{t('authentification.subtitle')} | ||
</Typography> | ||
<PasswordField | ||
id="authentification-password-field" | ||
disabled={isLoading} | ||
value={password} | ||
onChange={onPasswordChange} | ||
className="u-mv-1" | ||
label={t(`authentification.${isOIDC ? 'label-oidc' : 'label'}`)} | ||
variant="outlined" | ||
fullWidth | ||
error={Boolean(error) || error !== ''} | ||
helperText={error && t(`authentification.errors.${error}`)} | ||
/> | ||
<Typography | ||
variant="body1" | ||
component="a" | ||
color="primary" | ||
href={getPassphraseResetUrlCb} | ||
className="u-link" | ||
> | ||
{t('authentification.forgotten-password')} | ||
</Typography> | ||
</form> | ||
} | ||
actions={ | ||
<Buttons | ||
busy={isLoading} | ||
disabled={isLoading} | ||
onClick={handleAuthentification} | ||
label={t('authentification.unlock')} | ||
fullWidth | ||
/> | ||
} | ||
/> | ||
) | ||
} | ||
|
||
PermissionDialog.defaultProps = { | ||
isOIDC: false | ||
} | ||
|
||
PermissionDialog.propTypes = { | ||
/** Show closing button */ | ||
closable: PropTypes.bool, | ||
/** A function call on clicking the close button */ | ||
onDismiss: PropTypes.func, | ||
/** A function call on submitting the form with the password entered */ | ||
onAuthentification: PropTypes.func, | ||
/** Waiting status, e.g. processing of form submission */ | ||
isLoading: PropTypes.bool, | ||
/** Show specific wording for OIDC */ | ||
isOIDC: PropTypes.bool, | ||
/** Error key to display a message */ | ||
error: PropTypes.string | ||
} | ||
|
||
export default withSpecificDialogLocales(AuthentificationDialog) |
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,37 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import IconButton from '../../IconButton' | ||
import Icon from '../../Icon' | ||
import { useI18n } from '../../I18n' | ||
import InputAdornment from '../../InputAdornment' | ||
import EyeIcon from '../../Icons/Eye' | ||
import EyeClosedIcon from '../../Icons/EyeClosed' | ||
import withSpecificDialogLocales from './locales/withSpecificDialogLocales' | ||
|
||
/** | ||
* Icon button for hiding or showing something | ||
*/ | ||
const EyeAdornment = ({ hidden, ...rest }) => { | ||
const { t } = useI18n() | ||
|
||
return ( | ||
<InputAdornment position="end"> | ||
<IconButton | ||
className="u-ph-half u-mh-0 u-miw-auto" | ||
size="small" | ||
label={hidden ? t('eye-adornment.show') : t('eye-adornment.hide')} | ||
{...rest} | ||
> | ||
<Icon icon={hidden ? EyeIcon : EyeClosedIcon} color="white" /> | ||
</IconButton> | ||
</InputAdornment> | ||
) | ||
} | ||
|
||
EyeAdornment.propTypes = { | ||
/** The state of the element to be displayed */ | ||
hidden: PropTypes.bool | ||
} | ||
|
||
export default withSpecificDialogLocales(EyeAdornment) |
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, { useState } from 'react' | ||
|
||
import TextField from '../../MuiCozyTheme/TextField' | ||
import EyeAdornment from './EyeAdornment' | ||
|
||
/** | ||
* Password field with the option of hiding or displaying it | ||
*/ | ||
const PasswordField = props => { | ||
const [hidden, setHidden] = useState(true) | ||
|
||
return ( | ||
<TextField | ||
{...props} | ||
variant="outlined" | ||
type={hidden ? 'password' : 'text'} | ||
InputProps={{ | ||
endAdornment: ( | ||
<EyeAdornment | ||
onClick={() => setHidden(!hidden)} | ||
hidden={hidden} | ||
disabled={props.disabled} | ||
/> | ||
) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
export default PasswordField |
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,20 @@ | ||
{ | ||
"authentification": { | ||
"title": "Login", | ||
"title-oidc": "Cozy Pass", | ||
"subtitle": "For security, please confirm your identity", | ||
"label": "Cozy password", | ||
"label-oidc": "Cozy Pass password", | ||
"abort": "Leave", | ||
"unlock": "Unlock", | ||
"forgotten-password": "I forgot my password", | ||
"errors": { | ||
"invalid_password": "Incorrect password, try again.", | ||
"server": "Something went wrong with the server. Please, reload the page." | ||
} | ||
}, | ||
"eye-adornment": { | ||
"show": "show", | ||
"hide": "hide" | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"authentification": { | ||
"title": "Authentification", | ||
"title-oidc": "Cozy Pass", | ||
"subtitle": "Par sécurité, merci de confirmer votre identité", | ||
"label": "Mot de passe Cozy", | ||
"label-oidc": "Mot de passe Cozy Pass", | ||
"abort": "Quitter", | ||
"unlock": "Déverrouiller", | ||
"forgotten-password": "J'ai oublié mon mot de passe", | ||
"errors": { | ||
"invalid_password": "Mot de passe incorrect, essayer à nouveau.", | ||
"server": "Une erreur s'est produite. Merci de recharger la page." | ||
} | ||
}, | ||
"eye-adornment": { | ||
"show": "afficher", | ||
"hide": "masquer" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
react/CozyDialogs/specific/locales/withSpecificDialogLocales.jsx
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,11 @@ | ||
import withOnlyLocales from '../../../I18n/withOnlyLocales' | ||
|
||
import en from './en.json' | ||
import fr from './fr.json' | ||
|
||
export const locales = { | ||
en, | ||
fr | ||
} | ||
|
||
export default withOnlyLocales(locales) |
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