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
146 lines (139 loc) · 4.61 KB
/
TwoFactorAuthenticatePage.js
File metadata and controls
146 lines (139 loc) · 4.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { number, object } from 'yup'
import { Box, Heading, Stack, useToast } from '@chakra-ui/core'
import { useHistory, useLocation, useParams } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { Formik } from 'formik'
import { useUserVar } from './UserState'
import { AUTHENTICATE } from './graphql/mutations'
import AuthenticateField from './AuthenticateField'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
import { activate } from './i18n.config'
import { LoadingMessage } from './LoadingMessage'
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
export default function TwoFactorAuthenticatePage() {
const { login } = useUserVar()
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,
position: 'top-left',
})
},
onCompleted({ authenticate }) {
// User successfully completes tfa validation
if (authenticate.result.__typename === 'AuthResult') {
login({
jwt: authenticate.result.authToken,
tfaSendMethod: authenticate.result.user.tfaSendMethod,
userName: authenticate.result.user.userName,
})
if (authenticate.result.user.preferredLang === 'ENGLISH') activate('en')
else if (authenticate.result.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,
position: 'top-left',
})
}
// Non server error occurs
else if (authenticate.result.__typename === 'AuthenticateError') {
toast({
title: i18n._(
t`Unable to sign in to your account, please try again.`,
),
description: authenticate.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect authenticate.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect authenticate.result typename.')
}
},
})
if (loading) return <LoadingMessage />
if (error) return <ErrorFallbackMessage error={error} />
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>
)
}