|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { useHistory } from 'react-router-dom'; |
| 3 | +import { |
| 4 | + Button, |
| 5 | + Typography, |
| 6 | + makeStyles, |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + TextField, |
| 10 | + Grid, |
| 11 | + Snackbar, |
| 12 | +} from '@material-ui/core'; |
| 13 | +import { useFormik, FormikErrors } from 'formik'; |
| 14 | +import { SnackBarContentError } from './components/SnackBarContentError'; |
| 15 | +import { useAuth } from './components/AuthContext'; |
| 16 | +import { UnauthenticatedContainer } from './components/UnauthenticatedContainer'; |
| 17 | +import { accountsClient } from './accounts'; |
| 18 | + |
| 19 | +const useStyles = makeStyles((theme) => ({ |
| 20 | + cardContent: { |
| 21 | + padding: theme.spacing(3), |
| 22 | + }, |
| 23 | + divider: { |
| 24 | + marginTop: theme.spacing(2), |
| 25 | + marginBottom: theme.spacing(2), |
| 26 | + }, |
| 27 | + logo: { |
| 28 | + maxWidth: '100%', |
| 29 | + width: 250, |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +interface LoginMfaProps { |
| 34 | + mfaToken: string; |
| 35 | +} |
| 36 | + |
| 37 | +interface LoginMfaValues { |
| 38 | + code: string; |
| 39 | +} |
| 40 | + |
| 41 | +export const LoginMfa = ({ mfaToken }: LoginMfaProps) => { |
| 42 | + const classes = useStyles(); |
| 43 | + const history = useHistory(); |
| 44 | + const { loginWithService } = useAuth(); |
| 45 | + const [error, setError] = useState<string | undefined>(); |
| 46 | + const formik = useFormik<LoginMfaValues>({ |
| 47 | + initialValues: { |
| 48 | + code: '', |
| 49 | + }, |
| 50 | + validate: (values) => { |
| 51 | + const errors: FormikErrors<LoginMfaValues> = {}; |
| 52 | + if (!values.code) { |
| 53 | + errors.code = 'Required'; |
| 54 | + } |
| 55 | + return errors; |
| 56 | + }, |
| 57 | + onSubmit: async (values, { setSubmitting }) => { |
| 58 | + try { |
| 59 | + await loginWithService('mfa', { |
| 60 | + mfaToken, |
| 61 | + code: values.code, |
| 62 | + }); |
| 63 | + history.push('/'); |
| 64 | + } catch (error) { |
| 65 | + setError(error.message); |
| 66 | + setSubmitting(false); |
| 67 | + } |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + const fetchAuthenticators = async () => { |
| 73 | + // TODO try catch |
| 74 | + const data = await accountsClient.authenticatorsByMfaToken(mfaToken); |
| 75 | + const authenticator = data[0]; |
| 76 | + await accountsClient.mfaChallenge(mfaToken, authenticator.id); |
| 77 | + }; |
| 78 | + |
| 79 | + fetchAuthenticators(); |
| 80 | + }, [mfaToken]); |
| 81 | + |
| 82 | + return ( |
| 83 | + <UnauthenticatedContainer> |
| 84 | + <Snackbar |
| 85 | + anchorOrigin={{ |
| 86 | + vertical: 'top', |
| 87 | + horizontal: 'center', |
| 88 | + }} |
| 89 | + open={!!error} |
| 90 | + onClose={() => setError(undefined)} |
| 91 | + > |
| 92 | + <SnackBarContentError message={error} /> |
| 93 | + </Snackbar> |
| 94 | + |
| 95 | + <Card> |
| 96 | + <CardContent className={classes.cardContent}> |
| 97 | + <form onSubmit={formik.handleSubmit}> |
| 98 | + <Grid container spacing={3}> |
| 99 | + <Grid item xs={12}> |
| 100 | + <img src="/logo.png" alt="Logo" className={classes.logo} /> |
| 101 | + </Grid> |
| 102 | + <Grid item xs={12}> |
| 103 | + <Typography variant="h5">Two-factor authentication</Typography> |
| 104 | + </Grid> |
| 105 | + <Grid item xs={12}> |
| 106 | + <Typography variant="body2" color="textSecondary"> |
| 107 | + Your account is protected by two-factor authentication. To verify your identity |
| 108 | + you need to provide the access code from your authenticator app. |
| 109 | + </Typography> |
| 110 | + </Grid> |
| 111 | + <Grid item xs={12}> |
| 112 | + <TextField |
| 113 | + label="Authenticator code" |
| 114 | + variant="outlined" |
| 115 | + fullWidth={true} |
| 116 | + id="code" |
| 117 | + value={formik.values.code} |
| 118 | + onChange={formik.handleChange} |
| 119 | + error={Boolean(formik.errors.code && formik.touched.code)} |
| 120 | + helperText={formik.touched.code && formik.errors.code} |
| 121 | + /> |
| 122 | + </Grid> |
| 123 | + <Grid item xs={12} md={6}> |
| 124 | + <Button |
| 125 | + variant="contained" |
| 126 | + color="primary" |
| 127 | + type="submit" |
| 128 | + disabled={formik.isSubmitting} |
| 129 | + > |
| 130 | + Sign in |
| 131 | + </Button> |
| 132 | + </Grid> |
| 133 | + </Grid> |
| 134 | + </form> |
| 135 | + </CardContent> |
| 136 | + </Card> |
| 137 | + </UnauthenticatedContainer> |
| 138 | + ); |
| 139 | +}; |
0 commit comments