Skip to content

Commit d17197e

Browse files
committed
Early feedback
1 parent 686741e commit d17197e

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

packages-exp/auth-exp/src/core/strategies/abstract_popup_redirect_action.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '../../model/popup_redirect';
2424
import { User, UserCredential } from '../../model/user';
2525
import { AuthErrorCode } from '../errors';
26-
import { fail } from '../util/assert';
26+
import { debugAssert, fail } from '../util/assert';
2727
import { _link, _reauth, _signIn, IdpTask, IdpTaskParams } from './idp';
2828

2929
interface PendingPromise {
@@ -43,7 +43,7 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
4343

4444
constructor(
4545
protected readonly auth: Auth,
46-
readonly filter: AuthEventType,
46+
readonly filter: AuthEventType|'redirect',
4747
protected readonly resolver: PopupRedirectResolver,
4848
protected user?: User
4949
) {
@@ -55,16 +55,20 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
5555
return new Promise<UserCredential>(async (resolve, reject) => {
5656
this.pendingPromise = { resolve, reject };
5757

58-
this.eventManager = await this.resolver._initialize(this.auth);
59-
await this.onExecution();
60-
this.eventManager.registerConsumer(this);
58+
try {
59+
this.eventManager = await this.resolver._initialize(this.auth);
60+
await this.onExecution();
61+
this.eventManager.registerConsumer(this);
62+
} catch (e) {
63+
this.reject(e);
64+
}
6165
});
6266
}
6367

6468
async onAuthEvent(event: AuthEvent): Promise<void> {
6569
const { urlResponse, sessionId, postBody, tenantId, error, type } = event;
6670
if (error) {
67-
this.broadcastResult(null, error);
71+
this.reject(error);
6872
return;
6973
}
7074

@@ -78,19 +82,19 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
7882
};
7983

8084
try {
81-
this.broadcastResult(await this.getIdpTask(type)(params));
85+
this.resolve(await this.getIdpTask(type)(params));
8286
} catch (e) {
83-
this.broadcastResult(null, e);
87+
this.reject(e);
8488
}
8589
}
8690

8791
onError(error: FirebaseError): void {
88-
this.broadcastResult(null, error);
92+
this.reject(error);
8993
}
9094

91-
isMatchingEvent(eventId: string|null): boolean {
92-
return !!eventId && this.eventId === eventId;
93-
}
95+
// isMatchingEvent(eventId: string|null): boolean {
96+
// return !!eventId && this.eventId === eventId;
97+
// }
9498

9599
private getIdpTask(type: AuthEventType): IdpTask {
96100
switch(type) {
@@ -108,19 +112,24 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
108112
}
109113
}
110114

111-
protected broadcastResult(cred: UserCredential | null, error?: Error): void {
112-
if (this.pendingPromise) {
113-
if (error) {
114-
this.pendingPromise.reject(error);
115-
} else {
116-
this.pendingPromise.resolve(cred!);
117-
}
118-
}
115+
protected resolve(cred: UserCredential): void {
116+
debugAssert(this.pendingPromise, 'Pending promise was never set');
117+
this.pendingPromise.resolve(cred);
118+
this.unregisterAndCleanUp();
119+
}
119120

120-
this.pendingPromise = null;
121+
protected reject(error: Error): void {
122+
debugAssert(this.pendingPromise, 'Pending promise was never set');
123+
this.pendingPromise.reject(error);
124+
this.unregisterAndCleanUp();
125+
}
126+
127+
private unregisterAndCleanUp() {
121128
if (this.eventManager) {
122129
this.eventManager.unregisterConsumer(this);
123130
}
131+
132+
this.pendingPromise = null;
124133
this.cleanUp();
125134
}
126135

packages-exp/auth-exp/src/core/strategies/popup.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ class PopupAction extends AbstractPopupRedirectAction {
129129
}
130130

131131
cancel(): void {
132-
this.broadcastResult(
133-
null,
132+
this.reject(
134133
AUTH_ERROR_FACTORY.create(AuthErrorCode.EXPIRED_POPUP_REQUEST, {
135134
appName: this.auth.name
136135
})
@@ -159,8 +158,7 @@ class PopupAction extends AbstractPopupRedirectAction {
159158
// call could still be in flight.
160159
this.pollId = window.setTimeout(() => {
161160
this.pollId = null;
162-
this.broadcastResult(
163-
null,
161+
this.reject(
164162
AUTH_ERROR_FACTORY.create(AuthErrorCode.POPUP_CLOSED_BY_USER, {
165163
appName
166164
})

0 commit comments

Comments
 (0)