@@ -81,6 +81,8 @@ import { _logWarn } from '../util/log';
81
81
import { _getPasswordPolicy } from '../../api/password_policy/get_password_policy' ;
82
82
import { PasswordPolicyInternal } from '../../model/password_policy' ;
83
83
import { PasswordPolicyImpl } from './password_policy_impl' ;
84
+ import { getAccountInfo } from '../../api/account_management/account' ;
85
+ import { UserImpl } from '../user/user_impl' ;
84
86
85
87
interface AsyncAction {
86
88
( ) : Promise < void > ;
@@ -174,10 +176,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
174
176
}
175
177
}
176
178
177
- // Skip loading users from persistence in FirebaseServerApp Auth instances.
178
- if ( ! _isFirebaseServerApp ( this . app ) ) {
179
- await this . initializeCurrentUser ( popupRedirectResolver ) ;
180
- }
179
+ await this . initializeCurrentUser ( popupRedirectResolver ) ;
181
180
182
181
this . lastNotifiedUid = this . currentUser ?. uid || null ;
183
182
@@ -221,9 +220,47 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
221
220
await this . _updateCurrentUser ( user , /* skipBeforeStateCallbacks */ true ) ;
222
221
}
223
222
223
+ private async initializeCurrentUserFromIdToken (
224
+ idToken : string
225
+ ) : Promise < void > {
226
+ try {
227
+ const response = await getAccountInfo ( this , { idToken } ) ;
228
+ const user = await UserImpl . _fromGetAccountInfoResponse (
229
+ this ,
230
+ response ,
231
+ idToken
232
+ ) ;
233
+ await this . directlySetCurrentUser ( user ) ;
234
+ } catch ( err ) {
235
+ console . warn (
236
+ 'FirebaseServerApp could not login user with provided authIdToken: ' ,
237
+ err
238
+ ) ;
239
+ await this . directlySetCurrentUser ( null ) ;
240
+ }
241
+ }
242
+
224
243
private async initializeCurrentUser (
225
244
popupRedirectResolver ?: PopupRedirectResolver
226
245
) : Promise < void > {
246
+ if ( _isFirebaseServerApp ( this . app ) ) {
247
+ const idToken = this . app . settings . authIdToken ;
248
+ if ( idToken ) {
249
+ // Start the auth operation in the next tick to allow a moment for the customer's app to
250
+ // attach an emulator, if desired.
251
+ return new Promise < void > ( resolve => {
252
+ setTimeout ( ( ) =>
253
+ this . initializeCurrentUserFromIdToken ( idToken ) . then (
254
+ resolve ,
255
+ resolve
256
+ )
257
+ ) ;
258
+ } ) ;
259
+ } else {
260
+ return this . directlySetCurrentUser ( null ) ;
261
+ }
262
+ }
263
+
227
264
// First check to see if we have a pending redirect event.
228
265
const previouslyStoredUser =
229
266
( await this . assertedPersistence . getCurrentUser ( ) ) as UserInternal | null ;
0 commit comments