1
1
import React , { useContext , useState , useRef } from "react" ;
2
2
import { Redirect } from "react-router-dom" ;
3
- import { Box , Button , Flex , Input , Text , VStack } from "@chakra-ui/react" ;
3
+ import {
4
+ Box ,
5
+ Button ,
6
+ Center ,
7
+ Flex ,
8
+ Input ,
9
+ Spinner ,
10
+ Text ,
11
+ VStack ,
12
+ } from "@chakra-ui/react" ;
4
13
import authAPIClient from "../../APIClients/AuthAPIClient" ;
5
14
import AUTHENTICATED_USER_KEY from "../../constants/AuthConstants" ;
6
15
import { HOME_PAGE } from "../../constants/Routes" ;
7
16
import AuthContext from "../../contexts/AuthContext" ;
8
17
import { AuthenticatedUser } from "../../types/AuthTypes" ;
18
+ import CreateToast from "../common/Toasts" ;
19
+ import { isErrorResponse } from "../../helper/error" ;
9
20
10
- type AuthyProps = {
21
+ type TwoFaProps = {
11
22
email : string ;
12
23
password : string ;
13
24
token : string ;
14
25
toggle : boolean ;
15
26
} ;
16
27
17
- const Authy = ( {
28
+ const TwoFa = ( {
18
29
email,
19
30
password,
20
31
token,
21
32
toggle,
22
- } : AuthyProps ) : React . ReactElement => {
33
+ } : TwoFaProps ) : React . ReactElement => {
34
+ const newToast = CreateToast ( ) ;
23
35
const { authenticatedUser, setAuthenticatedUser } = useContext ( AuthContext ) ;
24
- const [ error , setError ] = useState ( "" ) ;
25
36
const [ authCode , setAuthCode ] = useState ( "" ) ;
26
37
38
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
39
+
27
40
const inputRef = useRef < HTMLInputElement > ( null ) ;
28
41
29
- const onAuthySubmit = async ( ) => {
30
- let authUser : AuthenticatedUser | null ;
42
+ const twoFaSubmit = async ( ) => {
43
+ // Uncomment this if Google/Outlook sign in is ever needed
44
+ // authUser = await authAPIClient.twoFaWithGoogle(authCode, token);
31
45
32
- if ( token ) {
33
- authUser = await authAPIClient . twoFaWithGoogle ( authCode , token ) ;
34
- } else {
35
- authUser = await authAPIClient . twoFa ( authCode , email , password ) ;
46
+ if ( authCode . length < 6 ) {
47
+ newToast (
48
+ "Authentication Failed" ,
49
+ "Please enter a 6 digit authentication code." ,
50
+ "error" ,
51
+ ) ;
52
+ return ;
36
53
}
37
54
38
- if ( authUser ) {
55
+ setIsLoading ( true ) ;
56
+ const authUser = await authAPIClient . twoFa ( authCode , email , password ) ;
57
+
58
+ if ( isErrorResponse ( authUser ) ) {
59
+ setIsLoading ( false ) ;
60
+ newToast ( "Authentication Failed" , authUser . errMessage , "error" ) ;
61
+ } else {
39
62
localStorage . setItem ( AUTHENTICATED_USER_KEY , JSON . stringify ( authUser ) ) ;
40
63
setAuthenticatedUser ( authUser ) ;
41
- } else {
42
- setError ( "Error: Invalid token" ) ;
43
64
}
44
65
} ;
45
66
@@ -76,8 +97,8 @@ const Authy = ({
76
97
< VStack width = "75%" align = "flex-start" gap = "3vh" >
77
98
< Text variant = "login" > One last step!</ Text >
78
99
< Text variant = "loginSecondary" >
79
- In order to protect your account, please enter the authorization
80
- code from the Twilio Authy application .
100
+ In order to protect your account, please enter the 6 digit
101
+ authentication code from the Authenticator extension .
81
102
</ Text >
82
103
< Flex direction = "row" width = "100%" justifyContent = "space-between" >
83
104
{ boxIndexes . map ( ( boxIndex ) => {
@@ -98,17 +119,30 @@ const Authy = ({
98
119
) ;
99
120
} ) }
100
121
</ Flex >
101
- < Button
102
- variant = "login"
103
- disabled = { authCode . length < 6 }
104
- _hover = { {
105
- background : "teal.500" ,
106
- transition :
107
- "transition: background-color 0.5s ease !important" ,
108
- } }
109
- >
110
- Authenticate
111
- </ Button >
122
+ { isLoading ? (
123
+ < Flex width = "100%" >
124
+ < Spinner
125
+ thickness = "4px"
126
+ speed = "0.65s"
127
+ emptyColor = "gray.200"
128
+ size = "lg"
129
+ margin = "0 auto"
130
+ textAlign = "center"
131
+ /> { " " }
132
+ </ Flex >
133
+ ) : (
134
+ < Button
135
+ variant = "login"
136
+ _hover = { {
137
+ background : "teal.500" ,
138
+ transition :
139
+ "transition: background-color 0.5s ease !important" ,
140
+ } }
141
+ onClick = { twoFaSubmit }
142
+ >
143
+ Authenticate
144
+ </ Button >
145
+ ) }
112
146
< Input
113
147
ref = { inputRef }
114
148
autoFocus
@@ -126,4 +160,4 @@ const Authy = ({
126
160
return < > </ > ;
127
161
} ;
128
162
129
- export default Authy ;
163
+ export default TwoFa ;
0 commit comments