import React, { useState } from 'react'; import classnames from 'classnames'; import PropTypes from 'prop-types'; import { Button, Grid, TextField, Typography } from '@material-ui/core'; import LockRounded from '@material-ui/icons/LockRounded'; import { useHistory } from 'react-router'; import { useCommonStyles } from '../../../common.styles'; import { useStyles } from './HostedAuth.styles'; import { Link } from 'react-router-dom'; import { GoogleSvg } from './Icons'; const PasswordAuth = ({ authDetails, passwordLogin, loadInitialData }) => { const commonStyles = useCommonStyles(); const styles = useStyles(); const history = useHistory(); const [username, setUsername] = useState(''); 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; } const user = { username, password }; const path = evt.target.action; try { await passwordLogin(path, user); await loadInitialData(); history.push(`/`); } 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 (
or