@@ -23,7 +23,7 @@ import {
23
23
} from '../../model/popup_redirect' ;
24
24
import { User , UserCredential } from '../../model/user' ;
25
25
import { AuthErrorCode } from '../errors' ;
26
- import { fail } from '../util/assert' ;
26
+ import { debugAssert , fail } from '../util/assert' ;
27
27
import { _link , _reauth , _signIn , IdpTask , IdpTaskParams } from './idp' ;
28
28
29
29
interface PendingPromise {
@@ -43,7 +43,7 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
43
43
44
44
constructor (
45
45
protected readonly auth : Auth ,
46
- readonly filter : AuthEventType ,
46
+ readonly filter : AuthEventType | 'redirect' ,
47
47
protected readonly resolver : PopupRedirectResolver ,
48
48
protected user ?: User
49
49
) {
@@ -55,16 +55,20 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
55
55
return new Promise < UserCredential > ( async ( resolve , reject ) => {
56
56
this . pendingPromise = { resolve, reject } ;
57
57
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
+ }
61
65
} ) ;
62
66
}
63
67
64
68
async onAuthEvent ( event : AuthEvent ) : Promise < void > {
65
69
const { urlResponse, sessionId, postBody, tenantId, error, type } = event ;
66
70
if ( error ) {
67
- this . broadcastResult ( null , error ) ;
71
+ this . reject ( error ) ;
68
72
return ;
69
73
}
70
74
@@ -78,19 +82,19 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
78
82
} ;
79
83
80
84
try {
81
- this . broadcastResult ( await this . getIdpTask ( type ) ( params ) ) ;
85
+ this . resolve ( await this . getIdpTask ( type ) ( params ) ) ;
82
86
} catch ( e ) {
83
- this . broadcastResult ( null , e ) ;
87
+ this . reject ( e ) ;
84
88
}
85
89
}
86
90
87
91
onError ( error : FirebaseError ) : void {
88
- this . broadcastResult ( null , error ) ;
92
+ this . reject ( error ) ;
89
93
}
90
94
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
+ // }
94
98
95
99
private getIdpTask ( type : AuthEventType ) : IdpTask {
96
100
switch ( type ) {
@@ -108,19 +112,24 @@ export abstract class AbstractPopupRedirectAction implements AuthEventConsumer {
108
112
}
109
113
}
110
114
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
+ }
119
120
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 ( ) {
121
128
if ( this . eventManager ) {
122
129
this . eventManager . unregisterConsumer ( this ) ;
123
130
}
131
+
132
+ this . pendingPromise = null ;
124
133
this . cleanUp ( ) ;
125
134
}
126
135
0 commit comments