-
Notifications
You must be signed in to change notification settings - Fork 891
/
popup_redirect.ts
110 lines (97 loc) · 3.42 KB
/
popup_redirect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as exp from '@firebase/auth/internal';
import { _isCordova, _isLikelyCordova } from './platform';
const _assert: typeof exp._assert = exp._assert;
/** Platform-agnostic popup-redirect resolver */
export class CompatPopupRedirectResolver
implements exp.PopupRedirectResolverInternal
{
// Create both resolvers for dynamic resolution later
private readonly browserResolver: exp.PopupRedirectResolverInternal =
exp._getInstance(exp.browserPopupRedirectResolver);
private readonly cordovaResolver: exp.PopupRedirectResolverInternal =
exp._getInstance(exp.cordovaPopupRedirectResolver);
// The actual resolver in use: either browserResolver or cordovaResolver.
private underlyingResolver: exp.PopupRedirectResolverInternal | null = null;
_redirectPersistence = exp.browserSessionPersistence;
_completeRedirectFn: (
auth: exp.Auth,
resolver: exp.PopupRedirectResolver,
bypassAuthState: boolean
) => Promise<exp.UserCredential | null> = exp._getRedirectResult;
_overrideRedirectResult = exp._overrideRedirectResult;
async _initialize(auth: exp.AuthImpl): Promise<exp.EventManager> {
await this.selectUnderlyingResolver();
return this.assertedUnderlyingResolver._initialize(auth);
}
async _openPopup(
auth: exp.AuthImpl,
provider: exp.AuthProvider,
authType: exp.AuthEventType,
eventId?: string
): Promise<exp.AuthPopup> {
await this.selectUnderlyingResolver();
return this.assertedUnderlyingResolver._openPopup(
auth,
provider,
authType,
eventId
);
}
async _openRedirect(
auth: exp.AuthImpl,
provider: exp.AuthProvider,
authType: exp.AuthEventType,
eventId?: string
): Promise<void> {
await this.selectUnderlyingResolver();
return this.assertedUnderlyingResolver._openRedirect(
auth,
provider,
authType,
eventId
);
}
_isIframeWebStorageSupported(
auth: exp.AuthImpl,
cb: (support: boolean) => unknown
): void {
this.assertedUnderlyingResolver._isIframeWebStorageSupported(auth, cb);
}
_originValidation(auth: exp.Auth): Promise<void> {
return this.assertedUnderlyingResolver._originValidation(auth);
}
get _shouldInitProactively(): boolean {
return _isLikelyCordova() || this.browserResolver._shouldInitProactively;
}
private get assertedUnderlyingResolver(): exp.PopupRedirectResolverInternal {
_assert(this.underlyingResolver, exp.AuthErrorCode.INTERNAL_ERROR);
return this.underlyingResolver;
}
private async selectUnderlyingResolver(): Promise<void> {
if (this.underlyingResolver) {
return;
}
// We haven't yet determined whether or not we're in Cordova; go ahead
// and determine that state now.
const isCordova = await _isCordova();
this.underlyingResolver = isCordova
? this.cordovaResolver
: this.browserResolver;
}
}