Skip to content

Commit 413f258

Browse files
committed
PR feedback
1 parent 480aa43 commit 413f258

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

packages-exp/auth-compat-exp/src/user_credential.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ import { Auth } from './auth';
2222
import { User } from './user';
2323
import { unwrap, wrapped } from './wrap';
2424

25-
const enum CredentialFrom {
26-
ERROR = 'credentialFromError',
27-
RESULT = 'credentialFromResult'
28-
}
29-
3025
function credentialFromResponse(
3126
userCredential: exp.UserCredentialInternal
3227
): exp.AuthCredential | null {
3328
return credentialFromObject(userCredential);
3429
}
3530

36-
function modifyError(auth: exp.Auth, e: FirebaseError): void {
31+
function attachExtraErrorFields(auth: exp.Auth, e: FirebaseError): void {
3732
// The response contains all fields from the server which may or may not
3833
// actually match the underlying type
3934
const response = ((e.customData as exp.TaggedWithTokenResponse | undefined)
@@ -66,11 +61,6 @@ function credentialFromObject(
6661
return null;
6762
}
6863

69-
const fn =
70-
object instanceof FirebaseError
71-
? CredentialFrom.ERROR
72-
: CredentialFrom.RESULT;
73-
7464
// Handle phone Auth credential responses, as they have a different format
7565
// from other backend responses (i.e. no providerId). This is also only the
7666
// case for user credentials (does not work for errors).
@@ -88,20 +78,20 @@ function credentialFromObject(
8878
return null;
8979
}
9080

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'>;
9682
switch (providerId) {
9783
case exp.ProviderId.GOOGLE:
98-
return exp.GoogleAuthProvider[fn](castObject);
84+
provider = exp.GoogleAuthProvider;
85+
break;
9986
case exp.ProviderId.FACEBOOK:
100-
return exp.FacebookAuthProvider[fn](castObject);
87+
provider = exp.FacebookAuthProvider;
88+
break;
10189
case exp.ProviderId.GITHUB:
102-
return exp.GithubAuthProvider[fn](castObject);
90+
provider = exp.GithubAuthProvider;
91+
break;
10392
case exp.ProviderId.TWITTER:
104-
return exp.TwitterAuthProvider[fn](castObject);
93+
provider = exp.TwitterAuthProvider;
94+
break;
10595
default:
10696
const {
10797
oauthIdToken,
@@ -139,6 +129,8 @@ function credentialFromObject(
139129
rawNonce: nonce
140130
});
141131
}
132+
133+
return object instanceof FirebaseError ? provider.credentialFromError(object) : provider.credentialFromResult(object);
142134
}
143135

144136
export async function convertCredential(
@@ -150,7 +142,7 @@ export async function convertCredential(
150142
credential = await credentialPromise;
151143
} catch (e) {
152144
if (e instanceof FirebaseError) {
153-
modifyError(auth, e);
145+
attachExtraErrorFields(auth, e);
154146
}
155147
throw e;
156148
}

0 commit comments

Comments
 (0)