3
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
5
import { RouteComponentProps , useLocation , useNavigate } from '@reach/router' ;
6
- import { useNavigateWithQuery } from '../../lib/hooks/useNavigateWithQuery' ;
7
6
import Index from '.' ;
8
7
import { IndexContainerProps , LocationState } from './interfaces' ;
9
8
import { useCallback , useEffect } from 'react' ;
@@ -36,7 +35,6 @@ export const IndexContainer = ({
36
35
// TODO, more strict validation for bad oauth params, FXA-11297
37
36
const authClient = useAuthClient ( ) ;
38
37
const navigate = useNavigate ( ) ;
39
- const navigateWithQuery = useNavigateWithQuery ( ) ;
40
38
const location = useLocation ( ) as ReturnType < typeof useLocation > & {
41
39
state ?: LocationState ;
42
40
} ;
@@ -49,25 +47,76 @@ export const IndexContainer = ({
49
47
const isWebChannelIntegration =
50
48
integration . isSync ( ) || integration . isDesktopRelay ( ) ;
51
49
52
- // 'email' query param followed by 'login_hint' should take precedence
53
- const email =
50
+ // 'email' query param should take precedence, followed by 'login_hint'
51
+ const suggestedEmail =
54
52
queryParamModel . email ||
55
53
integration . data . loginHint ||
56
54
currentAccount ( ) ?. email ;
57
- const shouldRedirectToSignin = email && ! prefillEmail ;
55
+
56
+ const hasEmailSuggestion = suggestedEmail && ! prefillEmail ;
57
+
58
+ const handleNavigation = useCallback (
59
+ (
60
+ exists : boolean ,
61
+ hasLinkedAccount : boolean ,
62
+ hasPassword : boolean ,
63
+ email : string
64
+ ) => {
65
+ const params = new URLSearchParams ( location . search ) ;
66
+ // We delete 'email' if it is present because otherwise, that param will take
67
+ // precedence and the user will be unable to create an account with a different
68
+ // email. Remove for signin as well as it is unnecessary. This is a byproduct
69
+ // of backwards compatibility between Backbone and React since we pass this
70
+ // param from content-server, TODO: FXA-10567
71
+ // We can also use useNavigateWithQuery after addressing the above.
72
+ params . delete ( 'email' ) ;
73
+ const hasParams = params . size > 0 ;
74
+ const isOAuth = location . pathname . startsWith ( '/oauth' ) ;
75
+ if ( ! exists ) {
76
+ const signupRoute = isOAuth ? '/oauth/signup' : '/signup' ;
77
+ console . log (
78
+ `${ signupRoute } ${ hasParams ? `?${ params . toString ( ) } ` : '' } `
79
+ ) ;
80
+ navigate ( `${ signupRoute } ${ hasParams ? `?${ params . toString ( ) } ` : '' } ` , {
81
+ state : {
82
+ email,
83
+ emailStatusChecked : true ,
84
+ } ,
85
+ } ) ;
86
+ } else {
87
+ const signinRoute = isOAuth ? '/oauth/signin' : '/signin' ;
88
+ navigate ( `${ signinRoute } ${ hasParams ? `?${ params . toString ( ) } ` : '' } ` , {
89
+ state : {
90
+ email,
91
+ hasLinkedAccount,
92
+ hasPassword,
93
+ } ,
94
+ } ) ;
95
+ }
96
+ } ,
97
+ [ location . pathname , location . search , navigate ]
98
+ ) ;
58
99
59
100
useEffect ( ( ) => {
60
- if ( shouldRedirectToSignin ) {
61
- const route = location . pathname . startsWith ( '/oauth' )
62
- ? '/oauth/signin'
63
- : '/signin' ;
64
- navigateWithQuery ( route , {
65
- state : {
66
- email,
67
- } ,
68
- } ) ;
101
+ if ( ! hasEmailSuggestion ) {
102
+ return ;
69
103
}
70
- } ) ;
104
+
105
+ const checkEmailAndNavigate = async ( ) => {
106
+ const { exists, hasLinkedAccount, hasPassword } =
107
+ await authClient . accountStatusByEmail ( suggestedEmail , {
108
+ thirdPartyAuthStatus : true ,
109
+ } ) ;
110
+
111
+ handleNavigation ( exists , hasLinkedAccount , hasPassword , suggestedEmail ) ;
112
+ } ;
113
+
114
+ checkEmailAndNavigate ( ) ;
115
+ } , [ authClient , handleNavigation , hasEmailSuggestion , suggestedEmail ] ) ;
116
+
117
+ useEffect ( ( ) => {
118
+ console . log ( 'Location state changed' , location . state ) ;
119
+ } , [ location . state ] ) ;
71
120
72
121
const signUpOrSignInHandler = useCallback (
73
122
async ( email : string ) => {
@@ -112,37 +161,13 @@ export const IndexContainer = ({
112
161
}
113
162
}
114
163
115
- const params = new URLSearchParams ( location . search ) ;
116
- // We delete 'email' if it is present because otherwise, that param will take
117
- // precedence and the user will be unable to create an account with a different
118
- // email. Remove for signin as well as it is unnecessary. This is a byproduct
119
- // of backwards compatibility between Backbone and React since we pass this
120
- // param from content-server, TODO: FXA-10567
121
- // We can also use useNavigateWithQuery after addressing the above.
122
- params . delete ( 'email' ) ;
123
- const hasParams = params . size > 0 ;
124
- if ( ! exists ) {
125
- navigate ( `/signup${ hasParams ? `?${ params . toString ( ) } ` : '' } ` , {
126
- state : {
127
- email,
128
- emailStatusChecked : true ,
129
- } ,
130
- } ) ;
131
- } else {
132
- navigate ( `/signin${ hasParams ? `?${ params . toString ( ) } ` : '' } ` , {
133
- state : {
134
- email,
135
- hasLinkedAccount,
136
- hasPassword,
137
- } ,
138
- } ) ;
139
- }
164
+ handleNavigation ( exists , hasLinkedAccount , hasPassword , email ) ;
140
165
return { error : null } ;
141
166
} catch ( error ) {
142
167
return getHandledError ( error ) ;
143
168
}
144
169
} ,
145
- [ authClient , navigate , isWebChannelIntegration , location . search ]
170
+ [ authClient , handleNavigation , isWebChannelIntegration ]
146
171
) ;
147
172
148
173
if ( validationError ) {
@@ -155,7 +180,7 @@ export const IndexContainer = ({
155
180
return < LoadingSpinner fullScreen /> ;
156
181
}
157
182
158
- if ( shouldRedirectToSignin ) {
183
+ if ( hasEmailSuggestion ) {
159
184
return < LoadingSpinner fullScreen /> ;
160
185
}
161
186
0 commit comments