Skip to content

Commit

Permalink
[Auth] Switch compat UserCredential function to use promise chaining …
Browse files Browse the repository at this point in the history
…instead of async/await (#5562)

* Update user_credential converter function to use .then chaining instead of async/await

* Formatting

* Changeset
  • Loading branch information
sam-gc committed Sep 30, 2021
1 parent 91117e0 commit f7d8324
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changeset/tame-owls-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/auth-compat": patch
"@firebase/auth": patch
---

Bugfix
55 changes: 28 additions & 27 deletions packages/auth-compat/src/user_credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function credentialFromResponse(
function attachExtraErrorFields(auth: exp.Auth, e: FirebaseError): void {
// The response contains all fields from the server which may or may not
// actually match the underlying type
const response = ((e.customData as exp.TaggedWithTokenResponse | undefined)
?._tokenResponse as unknown) as Record<string, string>;
const response = (e.customData as exp.TaggedWithTokenResponse | undefined)
?._tokenResponse as unknown as Record<string, string>;
if (e.code === 'auth/multi-factor-auth-required') {
const mfaErr = e as compat.MultiFactorError;
mfaErr.resolver = new MultiFactorResolver(
Expand All @@ -54,9 +54,9 @@ function attachExtraErrorFields(auth: exp.Auth, e: FirebaseError): void {
function credentialFromObject(
object: FirebaseError | exp.UserCredential
): exp.AuthCredential | null {
const { _tokenResponse } = (object instanceof FirebaseError
? object.customData
: object) as exp.TaggedWithTokenResponse;
const { _tokenResponse } = (
object instanceof FirebaseError ? object.customData : object
) as exp.TaggedWithTokenResponse;
if (!_tokenResponse) {
return null;
}
Expand Down Expand Up @@ -138,31 +138,32 @@ function credentialFromObject(
: provider.credentialFromResult(object);
}

export async function convertCredential(
export function convertCredential(
auth: exp.Auth,
credentialPromise: Promise<exp.UserCredential>
): Promise<compat.UserCredential> {
let credential: exp.UserCredential;
try {
credential = await credentialPromise;
} catch (e) {
if (e instanceof FirebaseError) {
attachExtraErrorFields(auth, e);
}
throw e;
}
const { operationType, user } = credential;

return {
operationType,
credential: credentialFromResponse(
credential as exp.UserCredentialInternal
),
additionalUserInfo: exp.getAdditionalUserInfo(
credential as exp.UserCredential
),
user: User.getOrCreate(user)
};
return credentialPromise
.catch(e => {
if (e instanceof FirebaseError) {
attachExtraErrorFields(auth, e);
}
throw e;
})
.then(credential => {
const operationType = credential.operationType;
const user = credential.user;

return {
operationType,
credential: credentialFromResponse(
credential as exp.UserCredentialInternal
),
additionalUserInfo: exp.getAdditionalUserInfo(
credential as exp.UserCredential
),
user: User.getOrCreate(user)
};
});
}

export async function convertConfirmationResult(
Expand Down

0 comments on commit f7d8324

Please sign in to comment.