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
132 lines (125 loc) · 4.35 KB
/
TwoFactorAuthenticatePage.js
File metadata and controls
132 lines (125 loc) · 4.35 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
import React from 'react'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Box, Button, Heading, Stack, useToast } from '@chakra-ui/react'
import { useHistory, useLocation, useParams } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { Formik } from 'formik'
import { LoadingMessage } from '../components/LoadingMessage'
import { AuthenticateField } from '../components/fields/AuthenticateField'
import { ErrorFallbackMessage } from '../components/ErrorFallbackMessage'
import { activate } from '../utilities/i18n.config'
import { useUserVar } from '../utilities/userState'
import { createValidationSchema } from '../utilities/fieldRequirements'
import { AUTHENTICATE } from '../graphql/mutations'
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 [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,
emailValidated: authenticate.result.user.emailValidated,
})
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={createValidationSchema(['twoFactorCode'])}
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 sendMethod={sendMethod} mb="4" />
<Stack align="center">
<Button variant="primary" isLoading={isSubmitting} type="submit">
<Trans>Submit</Trans>
</Button>
</Stack>
</form>
)}
</Formik>
</Box>
)
}