@@ -22,18 +22,13 @@ import { Auth } from './auth';
22
22
import { User } from './user' ;
23
23
import { unwrap , wrapped } from './wrap' ;
24
24
25
- const enum CredentialFrom {
26
- ERROR = 'credentialFromError' ,
27
- RESULT = 'credentialFromResult'
28
- }
29
-
30
25
function credentialFromResponse (
31
26
userCredential : exp . UserCredentialInternal
32
27
) : exp . AuthCredential | null {
33
28
return credentialFromObject ( userCredential ) ;
34
29
}
35
30
36
- function modifyError ( auth : exp . Auth , e : FirebaseError ) : void {
31
+ function attachExtraErrorFields ( auth : exp . Auth , e : FirebaseError ) : void {
37
32
// The response contains all fields from the server which may or may not
38
33
// actually match the underlying type
39
34
const response = ( ( e . customData as exp . TaggedWithTokenResponse | undefined )
@@ -66,11 +61,6 @@ function credentialFromObject(
66
61
return null ;
67
62
}
68
63
69
- const fn =
70
- object instanceof FirebaseError
71
- ? CredentialFrom . ERROR
72
- : CredentialFrom . RESULT ;
73
-
74
64
// Handle phone Auth credential responses, as they have a different format
75
65
// from other backend responses (i.e. no providerId). This is also only the
76
66
// case for user credentials (does not work for errors).
@@ -88,20 +78,20 @@ function credentialFromObject(
88
78
return null ;
89
79
}
90
80
91
- // We know for a fact that the function will match the value type
92
- // (based on the declaration of fn). We will therefore cast object to a
93
- // meaningless type to bypass the type system
94
- const castObject = object as exp . UserCredential & FirebaseError ;
95
-
81
+ let provider : Pick < typeof exp . OAuthProvider , 'credentialFromResult' | 'credentialFromError' > ;
96
82
switch ( providerId ) {
97
83
case exp . ProviderId . GOOGLE :
98
- return exp . GoogleAuthProvider [ fn ] ( castObject ) ;
84
+ provider = exp . GoogleAuthProvider ;
85
+ break ;
99
86
case exp . ProviderId . FACEBOOK :
100
- return exp . FacebookAuthProvider [ fn ] ( castObject ) ;
87
+ provider = exp . FacebookAuthProvider ;
88
+ break ;
101
89
case exp . ProviderId . GITHUB :
102
- return exp . GithubAuthProvider [ fn ] ( castObject ) ;
90
+ provider = exp . GithubAuthProvider ;
91
+ break ;
103
92
case exp . ProviderId . TWITTER :
104
- return exp . TwitterAuthProvider [ fn ] ( castObject ) ;
93
+ provider = exp . TwitterAuthProvider ;
94
+ break ;
105
95
default :
106
96
const {
107
97
oauthIdToken,
@@ -139,6 +129,8 @@ function credentialFromObject(
139
129
rawNonce : nonce
140
130
} ) ;
141
131
}
132
+
133
+ return object instanceof FirebaseError ? provider . credentialFromError ( object ) : provider . credentialFromResult ( object ) ;
142
134
}
143
135
144
136
export async function convertCredential (
@@ -150,7 +142,7 @@ export async function convertCredential(
150
142
credential = await credentialPromise ;
151
143
} catch ( e ) {
152
144
if ( e instanceof FirebaseError ) {
153
- modifyError ( auth , e ) ;
145
+ attachExtraErrorFields ( auth , e ) ;
154
146
}
155
147
throw e ;
156
148
}
0 commit comments