forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoFactorAuthenticatePage.js
More file actions
122 lines (115 loc) · 3.61 KB
/
TwoFactorAuthenticatePage.js
File metadata and controls
122 lines (115 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React from 'react'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { number, object } from 'yup'
import { Box, Heading, useToast, Stack } from '@chakra-ui/core'
import { useHistory, useParams, useLocation } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { Formik } from 'formik'
import { useUserState } from './UserState'
import { AUTHENTICATE } from './graphql/mutations'
import AuthenticateField from './AuthenticateField'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
import { activate } from './i18n.config'
export default function TwoFactorAuthenticatePage() {
const { login } = useUserState()
const history = useHistory()
const location = useLocation()
const toast = useToast()
const { i18n } = useLingui()
const { sendMethod, authenticateToken } = useParams()
const { from } = location.state || { from: { pathname: '/' } }
const validationSchema = object().shape({
twoFactorCode: number()
.typeError(i18n._(fieldRequirements.twoFactorCode.typeError))
.required(i18n._(fieldRequirements.twoFactorCode.required)),
})
const [authenticate, { loading, error }] = useMutation(AUTHENTICATE, {
onError() {
toast({
title: i18n._(t`An error occurred.`),
description: i18n._(
t`Unable to sign in to your account, please try again.`,
),
status: 'error',
duration: 9000,
isClosable: true,
})
},
onCompleted({ authenticate }) {
login({
jwt: authenticate.authResult.authToken,
tfaSendMethod: authenticate.authResult.user.tfaSendMethod,
userName: authenticate.authResult.user.userName,
})
if (authenticate.authResult.user.preferredLang === 'ENGLISH')
activate('en')
else if (authenticate.authResult.user.preferredLang === 'FRENCH')
activate('fr')
// // redirect to the home page.
history.replace(from)
// // Display a welcome message
toast({
title: i18n._(t`Sign In.`),
description: i18n._(t`Welcome, you are successfully signed in!`),
status: 'success',
duration: 9000,
isClosable: true,
})
},
})
if (loading)
return (
<p>
<Trans>Loading...</Trans>
</p>
)
if (error) return <p>{String(error)}</p>
return (
<Box w="100%">
<Formik
validationSchema={validationSchema}
initialValues={{
twoFactorCode: '',
authenticateToken: authenticateToken,
}}
onSubmit={async (values) => {
authenticate({
variables: {
authenticationCode: parseInt(values.twoFactorCode),
authenticateToken: values.authenticateToken,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form
onSubmit={handleSubmit}
role="form"
aria-label="form"
name="form"
>
<Heading as="h1" fontSize="2xl" mb="12" textAlign="center">
<Trans>Two Factor Authentication</Trans>
</Heading>
<AuthenticateField
name="twoFactorCode"
mb="4"
sendMethod={sendMethod}
/>
<Stack align="center">
<TrackerButton
variant="primary"
isLoading={isSubmitting}
type="submit"
>
<Trans>Submit</Trans>
</TrackerButton>
</Stack>
</form>
)}
</Formik>
</Box>
)
}