File tree Expand file tree Collapse file tree 3 files changed +16
-15
lines changed Expand file tree Collapse file tree 3 files changed +16
-15
lines changed Original file line number Diff line number Diff line change @@ -116,18 +116,15 @@ export class DatabaseService {
116
116
117
117
// eslint-disable-next-line @typescript-eslint/no-unused-vars
118
118
private onTokenChange ( _ : string ) : void {
119
- this . appInternal . INTERNAL . getToken ( )
120
- . then ( ( token ) => {
121
- const delayMillis = token . expirationTime - TOKEN_REFRESH_THRESHOLD_MILLIS - Date . now ( ) ;
122
- // If the new token is set to expire soon (unlikely), do nothing. Somebody will eventually
123
- // notice and refresh the token, at which point this callback will fire again.
124
- if ( delayMillis > 0 ) {
125
- this . scheduleTokenRefresh ( delayMillis ) ;
126
- }
127
- } )
128
- . catch ( ( err ) => {
129
- console . error ( 'Unexpected error while attempting to schedule a token refresh:' , err ) ;
130
- } ) ;
119
+ const token = this . appInternal . INTERNAL . getCachedToken ( ) ;
120
+ if ( token ) {
121
+ const delayMillis = token . expirationTime - TOKEN_REFRESH_THRESHOLD_MILLIS - Date . now ( ) ;
122
+ // If the new token is set to expire soon (unlikely), do nothing. Somebody will eventually
123
+ // notice and refresh the token, at which point this callback will fire again.
124
+ if ( delayMillis > 0 ) {
125
+ this . scheduleTokenRefresh ( delayMillis ) ;
126
+ }
127
+ }
131
128
}
132
129
133
130
private scheduleTokenRefresh ( delayMillis : number ) : void {
Original file line number Diff line number Diff line change @@ -74,6 +74,10 @@ export class FirebaseAppInternals {
74
74
return Promise . resolve ( this . cachedToken_ ) ;
75
75
}
76
76
77
+ public getCachedToken ( ) : FirebaseAccessToken | null {
78
+ return this . cachedToken_ || null ;
79
+ }
80
+
77
81
private refreshToken ( ) : Promise < FirebaseAccessToken > {
78
82
return Promise . resolve ( this . credential_ . getAccessToken ( ) )
79
83
. then ( ( result ) => {
@@ -97,6 +101,8 @@ export class FirebaseAppInternals {
97
101
if ( ! this . cachedToken_
98
102
|| this . cachedToken_ . accessToken !== token . accessToken
99
103
|| this . cachedToken_ . expirationTime !== token . expirationTime ) {
104
+ // Update the cache before firing listeners. Listeners may directly query the
105
+ // cached token state.
100
106
this . cachedToken_ = token ;
101
107
this . tokenListeners_ . forEach ( ( listener ) => {
102
108
listener ( token . accessToken ) ;
Original file line number Diff line number Diff line change @@ -211,9 +211,7 @@ describe('Database', () => {
211
211
} ) ;
212
212
} ) ;
213
213
214
- // Currently doesn't work as expected since onTokenChange() can force a token refresh
215
- // by calling getToken(). Skipping for now.
216
- xit ( 'should not reschedule when the token is about to expire in 5 minutes' , ( ) => {
214
+ it ( 'should not reschedule when the token is about to expire in 5 minutes' , ( ) => {
217
215
database . getDatabase ( ) ;
218
216
return mockApp . INTERNAL . getToken ( )
219
217
. then ( ( token1 ) => {
You can’t perform that action at this time.
0 commit comments