Skip to content

Commit d0ca061

Browse files
committed
PR feedback, fix compat tests
1 parent f78d73b commit d0ca061

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@ export function _getClientPlatform(): impl.ClientPlatform {
162162
return impl.ClientPlatform.BROWSER;
163163
}
164164

165+
/** Quick check that indicates the platform *may* be Cordova */
166+
export function _isLikelyCordova(): boolean {
167+
return _isAndroidOrIosCordovaScheme() && typeof document !== 'undefined';
168+
}
169+
165170
export async function _isCordova(): Promise<boolean> {
166-
if (!_isAndroidOrIosCordovaScheme() || typeof document === 'undefined') {
171+
if (!_isLikelyCordova()) {
167172
return false;
168173
}
169174

packages-exp/auth-compat-exp/src/popup_redirect.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,31 @@ describe('popup_redirect/CompatPopupRedirectResolver', () => {
126126
);
127127
});
128128
});
129+
130+
context('_shouldInitProactively', () => {
131+
it('returns true if platform may be cordova', () => {
132+
sinon.stub(platform, '_isLikelyCordova').returns(true);
133+
expect(compatResolver._shouldInitProactively).to.be.true;
134+
});
135+
136+
it('returns true if cordova is false but browser value is true', () => {
137+
sinon.stub(exp._getInstance<exp.PopupRedirectResolverInternal>(exp.browserPopupRedirectResolver), '_shouldInitProactively').value(true);
138+
sinon.stub(platform, '_isLikelyCordova').returns(false);
139+
expect(compatResolver._shouldInitProactively).to.be.true;
140+
});
141+
142+
it('returns false if not cordova and not browser early init', () => {
143+
sinon.stub(exp._getInstance<exp.PopupRedirectResolverInternal>(exp.browserPopupRedirectResolver), '_shouldInitProactively').value(false);
144+
sinon.stub(platform, '_isLikelyCordova').returns(false);
145+
expect(compatResolver._shouldInitProactively).to.be.false;
146+
});
147+
});
129148
});
130149

131150
class FakeResolver implements exp.PopupRedirectResolverInternal {
132151
_completeRedirectFn = async (): Promise<null> => null;
133152
_redirectPersistence = exp.inMemoryPersistence;
153+
_shouldInitProactively = true;
134154

135155
_initialize(): Promise<exp.EventManager> {
136156
throw new Error('Method not implemented.');

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717

1818
import * as exp from '@firebase/auth-exp/internal';
19-
import { _isCordova } from './platform';
19+
import { _isCordova, _isLikelyCordova } from './platform';
2020

2121
const _assert: typeof exp._assert = exp._assert;
22+
const BROWSER_RESOLVER: exp.PopupRedirectResolverInternal = exp._getInstance(exp.browserPopupRedirectResolver);
23+
const CORDOVA_RESOLVER: exp.PopupRedirectResolverInternal = exp._getInstance(exp.cordovaPopupRedirectResolver);
2224

2325
/** Platform-agnostic popup-redirect resolver */
2426
export class CompatPopupRedirectResolver
@@ -40,11 +42,7 @@ export class CompatPopupRedirectResolver
4042
// We haven't yet determined whether or not we're in Cordova; go ahead
4143
// and determine that state now.
4244
const isCordova = await _isCordova();
43-
this.underlyingResolver = exp._getInstance(
44-
isCordova
45-
? exp.cordovaPopupRedirectResolver
46-
: exp.browserPopupRedirectResolver
47-
);
45+
this.underlyingResolver = isCordova ? CORDOVA_RESOLVER : BROWSER_RESOLVER;
4846
return this.assertedUnderlyingResolver._initialize(auth);
4947
}
5048

@@ -87,6 +85,10 @@ export class CompatPopupRedirectResolver
8785
return this.assertedUnderlyingResolver._originValidation(auth);
8886
}
8987

88+
get _shouldInitProactively(): boolean {
89+
return _isLikelyCordova() || BROWSER_RESOLVER._shouldInitProactively;
90+
}
91+
9092
private get assertedUnderlyingResolver(): exp.PopupRedirectResolverInternal {
9193
_assert(this.underlyingResolver, exp.AuthErrorCode.INTERNAL_ERROR);
9294
return this.underlyingResolver;

packages-exp/auth-exp/test/helpers/redirect_persistence.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export class RedirectPersistence extends InMemoryPersistence {
2626
async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
2727
if (key.includes('pendingRedirect')) {
2828
return this.hasPendingRedirect.toString() as T;
29+
} else if (key.includes('redirectUser')) {
30+
return this.redirectUser as T | null;
2931
}
3032

31-
return this.redirectUser as T | null;
33+
throw new Error(`Unexpected redirect persistence key requested: ${key}`);
3234
}
3335
}

0 commit comments

Comments
 (0)