Skip to content

Commit b49a56e

Browse files
committed
Initial fixes
1 parent 506d557 commit b49a56e

File tree

10 files changed

+82
-25
lines changed

10 files changed

+82
-25
lines changed

packages-exp/auth-compat-exp/src/user_credential.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ export async function convertConfirmationResult(
116116
auth: externs.Auth,
117117
confirmationResultPromise: Promise<externs.ConfirmationResult>
118118
): Promise<compat.ConfirmationResult> {
119-
const { verificationId, confirm } = await confirmationResultPromise;
119+
const confirmationResultExp = await confirmationResultPromise;
120120
return {
121-
verificationId,
121+
verificationId: confirmationResultExp.verificationId,
122122
confirm: (verificationCode: string) =>
123-
convertCredential(auth, confirm(verificationCode))
123+
convertCredential(auth, confirmationResultExp.confirm(verificationCode))
124124
};
125125
}

packages-exp/auth-exp/src/api/authentication/custom_token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Auth } from '@firebase/auth-types-exp';
2121

2222
export interface SignInWithCustomTokenRequest {
2323
token: string;
24+
returnSecureToken: boolean;
2425
}
2526

2627
export interface SignInWithCustomTokenResponse extends IdTokenResponse {}

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class AuthImpl implements Auth, _FirebaseService {
107107
return;
108108
}
109109

110-
await this.initializeCurrentUser();
110+
await this.initializeCurrentUser(popupRedirectResolver);
111111

112112
if (this._deleted) {
113113
return;
@@ -151,7 +151,16 @@ export class AuthImpl implements Auth, _FirebaseService {
151151
this.notifyAuthListeners();
152152
}
153153

154-
private async initializeCurrentUser(): Promise<void> {
154+
private async initializeCurrentUser(popupRedirectResolver?: externs.PopupRedirectResolver): Promise<void> {
155+
// First check to see if we have a pending redirect event.
156+
if (popupRedirectResolver) {
157+
await this.getOrInitRedirectPersistenceManager();
158+
const result = await this._popupRedirectResolver!._completeRedirectFn(this, popupRedirectResolver);
159+
if (result) {
160+
return;
161+
};
162+
}
163+
155164
const storedUser = (await this.assertedPersistence.getCurrentUser()) as User | null;
156165
if (!storedUser) {
157166
return this.directlySetCurrentUser(storedUser);
@@ -227,10 +236,10 @@ export class AuthImpl implements Auth, _FirebaseService {
227236
}
228237
);
229238

230-
return this._updateCurrentUser(user && user._clone());
239+
return this._updateCurrentUser(user && user._clone(), false);
231240
}
232241

233-
async _updateCurrentUser(user: externs.User | null): Promise<void> {
242+
async _updateCurrentUser(user: externs.User | null, isInternal = true): Promise<void> {
234243
if (this._deleted) {
235244
return;
236245
}
@@ -241,10 +250,21 @@ export class AuthImpl implements Auth, _FirebaseService {
241250
{ appName: this.name }
242251
);
243252
}
244-
return this.queue(async () => {
253+
254+
const action = async (): Promise<void> => {
245255
await this.directlySetCurrentUser(user as User | null);
246256
this.notifyAuthListeners();
247-
});
257+
};
258+
259+
// Bypass the queue if this is an internal invocation and initialization is
260+
// incomplete. This state only occurs if there's a pending redirect
261+
// operation. That redirect operation was invoked from within the queue,
262+
// meaning that we're safe to directly set the user
263+
if (isInternal && !this._isInitialized) {
264+
return action();
265+
}
266+
267+
return this.queue(action);
248268
}
249269

250270
async signOut(): Promise<void> {
@@ -253,7 +273,7 @@ export class AuthImpl implements Auth, _FirebaseService {
253273
await this._setRedirectUser(null);
254274
}
255275

256-
return this._updateCurrentUser(null);
276+
return this._updateCurrentUser(null, false);
257277
}
258278

259279
setPersistence(persistence: externs.Persistence): Promise<void> {
@@ -333,8 +353,11 @@ export class AuthImpl implements Auth, _FirebaseService {
333353
}
334354

335355
async _redirectUserForId(id: string): Promise<User | null> {
336-
// Make sure we've cleared any pending ppersistence actions
337-
await this.queue(async () => {});
356+
// Make sure we've cleared any pending ppersistence actions if we're not in
357+
// the initializer
358+
if (this._isInitialized) {
359+
await this.queue(async () => {});
360+
}
338361

339362
if (this._currentUser?._redirectEventId === id) {
340363
return this._currentUser;
@@ -349,7 +372,11 @@ export class AuthImpl implements Auth, _FirebaseService {
349372

350373
async _persistUserIfCurrent(user: User): Promise<void> {
351374
if (user === this.currentUser) {
352-
return this.queue(async () => this.directlySetCurrentUser(user));
375+
if (!this._isInitialized) {
376+
return this.directlySetCurrentUser(user);
377+
} else {
378+
return this.queue(async () => this.directlySetCurrentUser(user));
379+
}
353380
}
354381
}
355382

packages-exp/auth-exp/src/core/strategies/custom_token.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export async function signInWithCustomToken(
2727
customToken: string
2828
): Promise<externs.UserCredential> {
2929
const response: IdTokenResponse = await getIdTokenResponse(authExtern, {
30-
token: customToken
30+
token: customToken,
31+
returnSecureToken: true,
3132
});
3233
const auth = _castAuth(authExtern);
3334
const cred = await UserCredentialImpl._fromIdTokenResponse(

packages-exp/auth-exp/src/core/user/additional_user_info.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ export function _fromIdTokenResponse(
4242
];
4343
if (signInProvider) {
4444
const filteredProviderId =
45-
providerId !== externs.ProviderId.ANONYMOUS &&
46-
providerId !== externs.ProviderId.CUSTOM
45+
signInProvider !== externs.ProviderId.ANONYMOUS &&
46+
signInProvider !== externs.ProviderId.CUSTOM
4747
? (signInProvider as externs.ProviderId)
4848
: null;
49+
console.log('In first branch');
4950
// Uses generic class in accordance with the legacy SDK.
5051
return new GenericAdditionalUserInfo(isNewUser, filteredProviderId);
5152
}
@@ -68,8 +69,10 @@ export function _fromIdTokenResponse(
6869
);
6970
case externs.ProviderId.CUSTOM:
7071
case externs.ProviderId.ANONYMOUS:
72+
console.log('In second branch');
7173
return new GenericAdditionalUserInfo(isNewUser, null);
7274
default:
75+
console.log('In third branch');
7376
return new GenericAdditionalUserInfo(isNewUser, providerId, profile);
7477
}
7578
}
@@ -79,7 +82,9 @@ class GenericAdditionalUserInfo implements externs.AdditionalUserInfo {
7982
readonly isNewUser: boolean,
8083
readonly providerId: externs.ProviderId | null,
8184
readonly profile: externs.UserProfile = {}
82-
) {}
85+
) {
86+
console.log('In constructor');
87+
}
8388
}
8489

8590
class FederatedAdditionalUserInfoWithUsername extends GenericAdditionalUserInfo {
@@ -140,5 +145,10 @@ export function getAdditionalUserInfo(
140145
};
141146
}
142147

143-
return _fromIdTokenResponse(_tokenResponse);
148+
149+
const result = _fromIdTokenResponse(_tokenResponse);
150+
if (result?.providerId === 'anonymous') {
151+
throw new Error(JSON.stringify(result));
152+
}
153+
return result;
144154
}

packages-exp/auth-exp/src/core/user/reload.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export async function _reloadWithoutSaving(user: User): Promise<void> {
4646
const newProviderData = coreAccount.providerUserInfo?.length
4747
? extractProviderData(coreAccount.providerUserInfo)
4848
: [];
49+
50+
const providerData = mergeProviderData(user.providerData, newProviderData);
51+
52+
const oldIsAnonymous = user.isAnonymous;
53+
const newIsAnonymous = !(user.email && coreAccount.passwordHash) && !(providerData?.length);
54+
const isAnonymous = !oldIsAnonymous ? false : newIsAnonymous;
55+
4956
const updates: Partial<User> = {
5057
uid: coreAccount.localId,
5158
displayName: coreAccount.displayName || null,
@@ -54,8 +61,9 @@ export async function _reloadWithoutSaving(user: User): Promise<void> {
5461
emailVerified: coreAccount.emailVerified || false,
5562
phoneNumber: coreAccount.phoneNumber || null,
5663
tenantId: coreAccount.tenantId || null,
57-
providerData: mergeProviderData(user.providerData, newProviderData),
58-
metadata: new UserMetadata(coreAccount.createdAt, coreAccount.lastLoginAt)
64+
providerData,
65+
metadata: new UserMetadata(coreAccount.createdAt, coreAccount.lastLoginAt),
66+
isAnonymous,
5967
};
6068

6169
Object.assign(user, updates);

packages-exp/auth-exp/src/core/user/token_manager.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Auth } from '../../model/auth';
2121
import { IdTokenResponse } from '../../model/id_token';
2222
import { AuthErrorCode } from '../errors';
2323
import { PersistedBlob } from '../persistence';
24-
import { assert, debugFail } from '../util/assert';
24+
import { assert, debugFail, fail } from '../util/assert';
2525

2626
/**
2727
* The number of milliseconds before the official expiration time of a token
@@ -56,14 +56,13 @@ export class StsTokenManager {
5656
return this.accessToken;
5757
}
5858

59-
if (!this.refreshToken) {
60-
assert(!this.accessToken, AuthErrorCode.TOKEN_EXPIRED, {
59+
if (this.accessToken && !this.refreshToken) {
60+
fail(AuthErrorCode.TOKEN_EXPIRED, {
6161
appName: auth.name
6262
});
63-
return null;
6463
}
6564

66-
await this.refresh(auth, this.refreshToken);
65+
await this.refresh(auth, this.refreshToken!);
6766
return this.accessToken;
6867
}
6968

packages-exp/auth-exp/src/model/popup_redirect.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@ export interface PopupRedirectResolver extends externs.PopupRedirectResolver {
9292
cb: (support: boolean) => unknown
9393
): void;
9494
_redirectPersistence: externs.Persistence;
95+
96+
// This is needed so that auth does not have a hard dependency on redirect
97+
_completeRedirectFn: (auth: externs.Auth, resolver: externs.PopupRedirectResolver) => Promise<externs.UserCredential | null>;
9598
}

packages-exp/auth-exp/src/platform_browser/popup_redirect.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { _setWindowLocation } from './auth_window';
4040
import { _openIframe } from './iframe/iframe';
4141
import { browserSessionPersistence } from './persistence/session_storage';
4242
import { _open, AuthPopup } from './util/popup';
43+
import { getRedirectResult } from './strategies/redirect';
4344

4445
/**
4546
* URL for Authentication widget which will initiate the OAuth handshake
@@ -190,6 +191,8 @@ class BrowserPopupRedirectResolver implements PopupRedirectResolver {
190191

191192
return this.originValidationPromises[key];
192193
}
194+
195+
_completeRedirectFn = getRedirectResult;
193196
}
194197

195198
export const browserPopupRedirectResolver: externs.PopupRedirectResolver = BrowserPopupRedirectResolver;

packages-exp/auth-exp/src/platform_browser/strategies/redirect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ export async function getRedirectResult(
103103
const auth = _castAuth(authExtern);
104104
const resolver = _withDefaultResolver(auth, resolverExtern);
105105
const action = new RedirectAction(auth, resolver);
106+
console.log('Executing redirect action');
106107
const result = await action.execute();
107108

108109
if (result) {
109110
delete result.user._redirectEventId;
111+
console.log('Persisting user');
110112
await auth._persistUserIfCurrent(result.user as User);
113+
console.log('Updating redirect user');
111114
await auth._setRedirectUser(null, resolverExtern);
115+
console.log('Finished');
112116
}
113117

118+
console.log('Finished with total');
114119
return result;
115120
}
116121

0 commit comments

Comments
 (0)