@@ -81,6 +81,8 @@ import { _logWarn } from '../util/log';
8181import { _getPasswordPolicy } from '../../api/password_policy/get_password_policy' ;
8282import { PasswordPolicyInternal } from '../../model/password_policy' ;
8383import { PasswordPolicyImpl } from './password_policy_impl' ;
84+ import { getAccountInfo } from '../../api/account_management/account' ;
85+ import { UserImpl } from '../user/user_impl' ;
8486
8587interface AsyncAction {
8688 ( ) : Promise < void > ;
@@ -174,10 +176,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
174176 }
175177 }
176178
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 ) ;
181180
182181 this . lastNotifiedUid = this . currentUser ?. uid || null ;
183182
@@ -221,9 +220,47 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
221220 await this . _updateCurrentUser ( user , /* skipBeforeStateCallbacks */ true ) ;
222221 }
223222
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+
224243 private async initializeCurrentUser (
225244 popupRedirectResolver ?: PopupRedirectResolver
226245 ) : 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+
227264 // First check to see if we have a pending redirect event.
228265 const previouslyStoredUser =
229266 ( await this . assertedPersistence . getCurrentUser ( ) ) as UserInternal | null ;
0 commit comments