1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/user/HostedAuth/HostedAuth.jsx

151 lines
5.3 KiB
React
Raw Normal View History

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';
import { useCommonStyles } from '../../../common.styles';
import { useStyles } from './HostedAuth.styles';
import useQueryParams from '../../../hooks/useQueryParams';
import AuthOptions from '../common/AuthOptions/AuthOptions';
import DividerText from '../../common/DividerText/DividerText';
import ConditionallyRender from '../../common/ConditionallyRender';
2022-01-04 13:30:25 +01:00
import PasswordField from '../../common/PasswordField/PasswordField';
2022-02-14 12:53:35 +01:00
import { useAuthApi } from '../../../hooks/api/actions/useAuthApi/useAuthApi';
import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser';
2022-02-14 13:08:49 +01:00
import {
LOGIN_BUTTON,
LOGIN_EMAIL_ID,
LOGIN_PASSWORD_ID,
} from '../../../testIds';
2021-04-29 21:55:48 +02:00
const HostedAuth = ({ authDetails, redirect }) => {
2021-04-29 21:55:48 +02:00
const commonStyles = useCommonStyles();
const styles = useStyles();
const { refetchUser } = useAuthUser();
2021-04-29 21:55:48 +02:00
const history = useHistory();
const params = useQueryParams();
2022-02-14 12:53:35 +01:00
const { passwordAuth } = useAuthApi();
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 {
await passwordAuth(authDetails.path, username, password);
refetchUser();
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 (
<>
<ConditionallyRender
condition={options.length > 0}
show={
<>
<AuthOptions options={options} />
<DividerText text="or signin with username" />
</>
}
/>
<ConditionallyRender
condition={!authDetails.defaultHidden}
show={
<form onSubmit={handleSubmit}>
<Typography
variant="subtitle2"
className={styles.apiError}
>
{apiError}
</Typography>
<div
className={classnames(
styles.contentContainer,
commonStyles.contentSpacingY
)}
>
<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-02-14 12:53:35 +01:00
data-test={LOGIN_EMAIL_ID}
/>
2022-01-04 13:30:25 +01:00
<PasswordField
label="Password"
onChange={evt => setPassword(evt.target.value)}
name="password"
value={password}
error={!!passwordError}
helperText={passwordError}
2022-02-14 12:53:35 +01:00
data-test={LOGIN_PASSWORD_ID}
/>
<Grid container>
<Button
variant="contained"
color="primary"
type="submit"
className={styles.button}
2022-02-14 13:08:49 +01:00
data-test={LOGIN_BUTTON}
>
Sign in
</Button>
</Grid>
</div>
</form>
}
/>
</>
2021-04-29 21:55:48 +02:00
);
};
HostedAuth.propTypes = {
2021-04-29 21:55:48 +02:00
authDetails: PropTypes.object.isRequired,
};
export default HostedAuth;