diff --git a/packages/auth/src/core/auth/auth_impl.ts b/packages/auth/src/core/auth/auth_impl.ts index 2103306bf5a..eb7e2a322c8 100644 --- a/packages/auth/src/core/auth/auth_impl.ts +++ b/packages/auth/src/core/auth/auth_impl.ts @@ -81,6 +81,8 @@ import { _logWarn } from '../util/log'; import { _getPasswordPolicy } from '../../api/password_policy/get_password_policy'; import { PasswordPolicyInternal } from '../../model/password_policy'; import { PasswordPolicyImpl } from './password_policy_impl'; +import { getAccountInfo } from '../../api/account_management/account'; +import { UserImpl } from '../user/user_impl'; interface AsyncAction { (): Promise; @@ -174,10 +176,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService { } } - // Skip loading users from persistence in FirebaseServerApp Auth instances. - if (!_isFirebaseServerApp(this.app)) { - await this.initializeCurrentUser(popupRedirectResolver); - } + await this.initializeCurrentUser(popupRedirectResolver); this.lastNotifiedUid = this.currentUser?.uid || null; @@ -221,9 +220,47 @@ export class AuthImpl implements AuthInternal, _FirebaseService { await this._updateCurrentUser(user, /* skipBeforeStateCallbacks */ true); } + private async initializeCurrentUserFromIdToken( + idToken: string + ): Promise { + try { + const response = await getAccountInfo(this, { idToken }); + const user = await UserImpl._fromGetAccountInfoResponse( + this, + response, + idToken + ); + await this.directlySetCurrentUser(user); + } catch (err) { + console.warn( + 'FirebaseServerApp could not login user with provided authIdToken: ', + err + ); + await this.directlySetCurrentUser(null); + } + } + private async initializeCurrentUser( popupRedirectResolver?: PopupRedirectResolver ): Promise { + if (_isFirebaseServerApp(this.app)) { + const idToken = this.app.settings.authIdToken; + if (idToken) { + // Start the auth operation in the next tick to allow a moment for the customer's app to + // attach an emulator, if desired. + return new Promise(resolve => { + setTimeout(() => + this.initializeCurrentUserFromIdToken(idToken).then( + resolve, + resolve + ) + ); + }); + } else { + return this.directlySetCurrentUser(null); + } + } + // First check to see if we have a pending redirect event. const previouslyStoredUser = (await this.assertedPersistence.getCurrentUser()) as UserInternal | null; diff --git a/packages/auth/src/core/auth/initialize.ts b/packages/auth/src/core/auth/initialize.ts index 3bb21364718..c6218953508 100644 --- a/packages/auth/src/core/auth/initialize.ts +++ b/packages/auth/src/core/auth/initialize.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { _getProvider, _isFirebaseServerApp, FirebaseApp } from '@firebase/app'; +import { _getProvider, FirebaseApp } from '@firebase/app'; import { deepEqual } from '@firebase/util'; import { Auth, Dependencies } from '../../model/public_types'; @@ -23,9 +23,7 @@ import { AuthErrorCode } from '../errors'; import { PersistenceInternal } from '../persistence'; import { _fail } from '../util/assert'; import { _getInstance } from '../util/instantiator'; -import { AuthImpl, _castAuth } from './auth_impl'; -import { UserImpl } from '../user/user_impl'; -import { getAccountInfo } from '../../api/account_management/account'; +import { AuthImpl } from './auth_impl'; /** * Initializes an {@link Auth} instance with fine-grained control over @@ -67,40 +65,9 @@ export function initializeAuth(app: FirebaseApp, deps?: Dependencies): Auth { const auth = provider.initialize({ options: deps }) as AuthImpl; - if (_isFirebaseServerApp(app)) { - if (app.settings.authIdToken !== undefined) { - const idToken = app.settings.authIdToken; - // Start the auth operation in the next tick to allow a moment for the customer's app to - // attach an emulator, if desired. - setTimeout(() => void _loadUserFromIdToken(auth, idToken), 0); - } - } - return auth; } -export async function _loadUserFromIdToken( - auth: Auth, - idToken: string -): Promise { - try { - const response = await getAccountInfo(auth, { idToken }); - const authInternal = _castAuth(auth); - await authInternal._initializationPromise; - const user = await UserImpl._fromGetAccountInfoResponse( - authInternal, - response, - idToken - ); - await authInternal._updateCurrentUser(user); - } catch (err) { - console.warn( - 'FirebaseServerApp could not login user with provided authIdToken: ', - err - ); - } -} - export function _initializeAuthInstance( auth: AuthImpl, deps?: Dependencies