Skip to content

Commit 1227182

Browse files
committed
Fix?
1 parent 003b3c7 commit 1227182

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ import { _logWarn } from '../util/log';
8181
import { _getPasswordPolicy } from '../../api/password_policy/get_password_policy';
8282
import { PasswordPolicyInternal } from '../../model/password_policy';
8383
import { PasswordPolicyImpl } from './password_policy_impl';
84+
import { getAccountInfo } from '../../api/account_management/account';
85+
import { UserImpl } from '../user/user_impl';
8486

8587
interface AsyncAction {
8688
(): Promise<void>;
@@ -92,6 +94,28 @@ export const enum DefaultConfig {
9294
API_SCHEME = 'https'
9395
}
9496

97+
98+
async function loadUserFromIdToken(
99+
auth: AuthInternal,
100+
idToken: string
101+
): Promise<UserInternal|null> {
102+
try {
103+
const response = await getAccountInfo(auth, { idToken });
104+
const user = await UserImpl._fromGetAccountInfoResponse(
105+
auth,
106+
response,
107+
idToken
108+
);
109+
return user;
110+
} catch (err) {
111+
console.warn(
112+
'FirebaseServerApp could not login user with provided authIdToken: ',
113+
err
114+
);
115+
return null;
116+
}
117+
}
118+
95119
export class AuthImpl implements AuthInternal, _FirebaseService {
96120
currentUser: User | null = null;
97121
emulatorConfig: EmulatorConfig | null = null;
@@ -174,10 +198,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
174198
}
175199
}
176200

177-
// Skip loading users from persistence in FirebaseServerApp Auth instances.
178-
if (!_isFirebaseServerApp(this.app)) {
179-
await this.initializeCurrentUser(popupRedirectResolver);
180-
}
201+
await this.initializeCurrentUser(popupRedirectResolver);
181202

182203
this.lastNotifiedUid = this.currentUser?.uid || null;
183204

@@ -248,6 +269,21 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
248269
}
249270
}
250271

272+
if (_isFirebaseServerApp(this.app)) {
273+
if (this.app.settings.authIdToken !== undefined) {
274+
const idToken = this.app.settings.authIdToken;
275+
// Start the auth operation in the next tick to allow a moment for the customer's app to
276+
// attach an emulator, if desired.
277+
setTimeout(() =>
278+
void loadUserFromIdToken(this, idToken).then(user =>
279+
void this.directlySetCurrentUser(user)
280+
),
281+
0
282+
);
283+
return;
284+
}
285+
}
286+
251287
// If no user in persistence, there is no current user. Set to null.
252288
if (!futureCurrentUser) {
253289
return this.directlySetCurrentUser(null);

packages/auth/src/core/auth/initialize.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { _getProvider, _isFirebaseServerApp, FirebaseApp } from '@firebase/app';
18+
import { _getProvider, FirebaseApp } from '@firebase/app';
1919
import { deepEqual } from '@firebase/util';
2020
import { Auth, Dependencies } from '../../model/public_types';
2121

2222
import { AuthErrorCode } from '../errors';
2323
import { PersistenceInternal } from '../persistence';
2424
import { _fail } from '../util/assert';
2525
import { _getInstance } from '../util/instantiator';
26-
import { AuthImpl, _castAuth } from './auth_impl';
27-
import { UserImpl } from '../user/user_impl';
28-
import { getAccountInfo } from '../../api/account_management/account';
26+
import { AuthImpl } from './auth_impl';
2927

3028
/**
3129
* Initializes an {@link Auth} instance with fine-grained control over
@@ -67,40 +65,9 @@ export function initializeAuth(app: FirebaseApp, deps?: Dependencies): Auth {
6765

6866
const auth = provider.initialize({ options: deps }) as AuthImpl;
6967

70-
if (_isFirebaseServerApp(app)) {
71-
if (app.settings.authIdToken !== undefined) {
72-
const idToken = app.settings.authIdToken;
73-
// Start the auth operation in the next tick to allow a moment for the customer's app to
74-
// attach an emulator, if desired.
75-
setTimeout(() => void _loadUserFromIdToken(auth, idToken), 0);
76-
}
77-
}
78-
7968
return auth;
8069
}
8170

82-
export async function _loadUserFromIdToken(
83-
auth: Auth,
84-
idToken: string
85-
): Promise<void> {
86-
try {
87-
const response = await getAccountInfo(auth, { idToken });
88-
const authInternal = _castAuth(auth);
89-
await authInternal._initializationPromise;
90-
const user = await UserImpl._fromGetAccountInfoResponse(
91-
authInternal,
92-
response,
93-
idToken
94-
);
95-
await authInternal._updateCurrentUser(user);
96-
} catch (err) {
97-
console.warn(
98-
'FirebaseServerApp could not login user with provided authIdToken: ',
99-
err
100-
);
101-
}
102-
}
103-
10471
export function _initializeAuthInstance(
10572
auth: AuthImpl,
10673
deps?: Dependencies

0 commit comments

Comments
 (0)