Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Auth] Switch compat UserCredential function to use promise chaining instead of async/await #5562

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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