2022-01-04 13:30:25 +01:00
|
|
|
import { useState } from 'react';
|
2021-04-29 21:55:48 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-01-04 13:30:25 +01:00
|
|
|
import { Button, Grid, TextField, Typography } from '@material-ui/core';
|
2021-04-29 21:55:48 +02:00
|
|
|
import { useHistory } from 'react-router';
|
2022-04-01 10:28:15 +02:00
|
|
|
import { useCommonStyles } from 'themes/commonStyles';
|
2021-04-29 21:55:48 +02:00
|
|
|
import { useStyles } from './HostedAuth.styles';
|
2022-03-28 10:49:59 +02:00
|
|
|
import useQueryParams from 'hooks/useQueryParams';
|
2021-05-18 12:59:48 +02:00
|
|
|
import AuthOptions from '../common/AuthOptions/AuthOptions';
|
2022-03-28 10:49:59 +02:00
|
|
|
import DividerText from 'component/common/DividerText/DividerText';
|
|
|
|
import ConditionallyRender from 'component/common/ConditionallyRender';
|
|
|
|
import PasswordField from 'component/common/PasswordField/PasswordField';
|
|
|
|
import { useAuthApi } from 'hooks/api/actions/useAuthApi/useAuthApi';
|
|
|
|
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
|
2022-04-01 10:28:15 +02:00
|
|
|
import { LOGIN_BUTTON, LOGIN_EMAIL_ID, LOGIN_PASSWORD_ID } from 'utils/testIds';
|
2021-04-29 21:55:48 +02:00
|
|
|
|
2022-02-21 14:05:11 +01:00
|
|
|
const HostedAuth = ({ authDetails, redirect }) => {
|
2021-04-29 21:55:48 +02:00
|
|
|
const commonStyles = useCommonStyles();
|
|
|
|
const styles = useStyles();
|
2022-02-10 17:04:10 +01:00
|
|
|
const { refetchUser } = useAuthUser();
|
2021-04-29 21:55:48 +02:00
|
|
|
const history = useHistory();
|
2021-05-08 18:46:05 +02:00
|
|
|
const params = useQueryParams();
|
2022-02-14 12:53:35 +01:00
|
|
|
const { passwordAuth } = useAuthApi();
|
2021-05-08 18:46:05 +02:00
|
|
|
const [username, setUsername] = useState(params.get('email') || '');
|
2021-04-29 21:55:48 +02:00
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
const [errors, setErrors] = useState({
|
|
|
|
usernameError: '',
|
|
|
|
passwordError: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleSubmit = async evt => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
if (!username) {
|
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
usernameError: 'This is a required field',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (!password) {
|
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
passwordError: 'This is a required field',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password || !username) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-10 17:04:10 +01:00
|
|
|
await passwordAuth(authDetails.path, username, password);
|
|
|
|
refetchUser();
|
2022-02-21 14:05:11 +01:00
|
|
|
history.push(redirect);
|
2021-04-29 21:55:48 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 404 || error.statusCode === 400) {
|
|
|
|
setErrors(prev => ({
|
|
|
|
...prev,
|
|
|
|
apiError: 'Invalid login details',
|
|
|
|
}));
|
|
|
|
setPassword('');
|
|
|
|
setUsername('');
|
|
|
|
} else {
|
|
|
|
setErrors({
|
|
|
|
apiError: 'Unknown error while trying to authenticate.',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const { usernameError, passwordError, apiError } = errors;
|
|
|
|
const { options = [] } = authDetails;
|
|
|
|
|
|
|
|
return (
|
2021-05-21 14:01:28 +02:00
|
|
|
<>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={options.length > 0}
|
|
|
|
show={
|
|
|
|
<>
|
|
|
|
<AuthOptions options={options} />
|
|
|
|
<DividerText text="or signin with username" />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
|
2021-10-19 13:08:25 +02:00
|
|
|
<ConditionallyRender
|
2022-01-26 13:28:51 +01:00
|
|
|
condition={!authDetails.defaultHidden}
|
2021-10-19 13:08:25 +02:00
|
|
|
show={
|
2022-02-10 17:04:10 +01:00
|
|
|
<form onSubmit={handleSubmit}>
|
2021-10-19 13:08:25 +02:00
|
|
|
<Typography
|
|
|
|
variant="subtitle2"
|
|
|
|
className={styles.apiError}
|
|
|
|
>
|
|
|
|
{apiError}
|
|
|
|
</Typography>
|
|
|
|
<div
|
|
|
|
className={classnames(
|
|
|
|
styles.contentContainer,
|
|
|
|
commonStyles.contentSpacingY
|
|
|
|
)}
|
2021-05-21 14:01:28 +02:00
|
|
|
>
|
2021-10-19 13:08:25 +02:00
|
|
|
<TextField
|
|
|
|
label="Username or email"
|
|
|
|
name="username"
|
|
|
|
type="string"
|
|
|
|
onChange={evt => setUsername(evt.target.value)}
|
|
|
|
value={username}
|
|
|
|
error={!!usernameError}
|
|
|
|
helperText={usernameError}
|
|
|
|
variant="outlined"
|
|
|
|
size="small"
|
2022-04-08 13:13:45 +02:00
|
|
|
data-testid={LOGIN_EMAIL_ID}
|
2021-10-19 13:08:25 +02:00
|
|
|
/>
|
2022-01-04 13:30:25 +01:00
|
|
|
<PasswordField
|
2021-10-19 13:08:25 +02:00
|
|
|
label="Password"
|
|
|
|
onChange={evt => setPassword(evt.target.value)}
|
|
|
|
name="password"
|
|
|
|
value={password}
|
|
|
|
error={!!passwordError}
|
|
|
|
helperText={passwordError}
|
2022-04-08 13:13:45 +02:00
|
|
|
data-testid={LOGIN_PASSWORD_ID}
|
2021-10-19 13:08:25 +02:00
|
|
|
/>
|
|
|
|
<Grid container>
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
|
|
|
className={styles.button}
|
2022-04-08 13:13:45 +02:00
|
|
|
data-testid={LOGIN_BUTTON}
|
2021-10-19 13:08:25 +02:00
|
|
|
>
|
|
|
|
Sign in
|
|
|
|
</Button>
|
|
|
|
</Grid>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
}
|
|
|
|
/>
|
2021-05-21 14:01:28 +02:00
|
|
|
</>
|
2021-04-29 21:55:48 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-21 14:01:28 +02:00
|
|
|
HostedAuth.propTypes = {
|
2021-04-29 21:55:48 +02:00
|
|
|
authDetails: PropTypes.object.isRequired,
|
2022-02-22 13:55:47 +01:00
|
|
|
redirect: PropTypes.string.isRequired,
|
2021-04-29 21:55:48 +02:00
|
|
|
};
|
|
|
|
|
2021-05-21 14:01:28 +02:00
|
|
|
export default HostedAuth;
|