Skip to content

Commit 2cff3e5

Browse files
committed
Add platform specific persistence validation to auth-compat-exp
1 parent 3fa6502 commit 2cff3e5

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

packages-exp/auth-compat-exp/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { version } from './package.json';
2929
import { Auth } from './src/auth';
3030
import { RecaptchaVerifier } from './src/recaptcha_verifier';
3131
import { EmailAuthProvider } from './src/email_auth_provider';
32+
import { Persistence } from './src/persistence';
3233

3334
const AUTH_TYPE = 'auth';
3435

@@ -68,11 +69,7 @@ function registerAuth(instance: _FirebaseNamespace): void {
6869
RecaptchaVerifier,
6970
TwitterAuthProvider: impl.TwitterAuthProvider,
7071
Auth: {
71-
Persistence: {
72-
LOCAL: 'LOCAL',
73-
NONE: 'NONE',
74-
SESSION: 'SESSION'
75-
}
72+
Persistence
7673
}
7774
// 'AuthCredential': fireauth.AuthCredential,
7875
// 'Error': fireauth.AuthError

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ import * as impl from '@firebase/auth-exp/internal';
2020
import * as compat from '@firebase/auth-types';
2121
import * as externs from '@firebase/auth-types-exp';
2222
import '@firebase/installations';
23-
import { Observer, Unsubscribe, ErrorFn } from '@firebase/util';
23+
import {
24+
Observer,
25+
Unsubscribe,
26+
ErrorFn,
27+
isIndexedDBAvailable
28+
} from '@firebase/util';
2429
import { User } from './user';
2530
import {
2631
convertConfirmationResult,
2732
convertCredential
2833
} from './user_credential';
2934
import { _isPopupRedirectSupported, _getClientPlatform } from './platform';
35+
import { Persistence, _validatePersistenceArgument } from './persistence';
3036

3137
export class Auth extends impl.AuthImplCompat<User>
3238
implements compat.FirebaseAuth {
@@ -164,12 +170,15 @@ export class Auth extends impl.AuthImplCompat<User>
164170
auth: externs.Auth,
165171
persistenceCompat: string
166172
): externs.Persistence {
173+
_validatePersistenceArgument(auth, persistence);
167174
switch (persistenceCompat) {
168-
case 'LOCAL':
169-
return impl.browserLocalPersistence;
170-
case 'SESSION':
175+
case Persistence.SESSION:
171176
return impl.browserSessionPersistence;
172-
case 'NONE':
177+
case Persistence.LOCAL:
178+
return isIndexedDBAvailable()
179+
? impl.indexedDBLocalPersistence
180+
: impl.browserLocalPersistence;
181+
case Persistence.NONE:
173182
return impl.inMemoryPersistence;
174183
default:
175184
return impl.fail(auth.name, impl.AuthErrorCode.ARGUMENT_ERROR);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { assertFn, AuthErrorCode } from '@firebase/auth-exp/internal';
19+
import * as externs from '@firebase/auth-types-exp';
20+
import { isIndexedDBAvailable, isNode, isReactNative } from '@firebase/util';
21+
import { _isWebStorageSupported, _isWorker } from './platform';
22+
23+
export const Persistence = {
24+
LOCAL: 'LOCAL',
25+
NONE: 'NONE',
26+
SESSION: 'SESSION'
27+
};
28+
29+
/**
30+
* Validates that an argument is a valid persistence value. If an invalid type
31+
* is specified, an error is thrown synchronously.
32+
*/
33+
export function _validatePersistenceArgument(
34+
auth: externs.Auth,
35+
persistence: string
36+
): void {
37+
assertFn(
38+
Object.values(Persistence).includes(persistence),
39+
auth.name,
40+
AuthErrorCode.INVALID_PERSISTENCE
41+
);
42+
// Validate if the specified type is supported in the current environment.
43+
if (isReactNative()) {
44+
// This is only supported in a browser.
45+
assertFn(
46+
persistence !== Persistence.SESSION,
47+
auth.name,
48+
AuthErrorCode.UNSUPPORTED_PERSISTENCE
49+
);
50+
return;
51+
}
52+
if (isNode()) {
53+
// Only none is supported in Node.js.
54+
assertFn(
55+
persistence === Persistence.NONE,
56+
auth.name,
57+
AuthErrorCode.UNSUPPORTED_PERSISTENCE
58+
);
59+
return;
60+
}
61+
if (_isWorker()) {
62+
// In a worker environment, either LOCAL or NONE are supported.
63+
// If indexedDB not supported and LOCAL provided, throw an error
64+
assertFn(
65+
persistence === Persistence.NONE ||
66+
(persistence === Persistence.LOCAL && isIndexedDBAvailable()),
67+
auth.name,
68+
AuthErrorCode.UNSUPPORTED_PERSISTENCE
69+
);
70+
return;
71+
}
72+
// This is restricted by what the browser supports.
73+
assertFn(
74+
persistence === Persistence.NONE || _isWebStorageSupported(),
75+
auth.name,
76+
AuthErrorCode.UNSUPPORTED_PERSISTENCE
77+
);
78+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function _isLocalStorageNotSynchronized(ua: string = getUA()): boolean {
8989
}
9090

9191
/** @return {boolean} Whether web storage is supported. */
92-
function _isWebStorageSupported(): boolean {
92+
export function _isWebStorageSupported(): boolean {
9393
try {
9494
const storage = self.localStorage;
9595
const key = impl._generateEventId();
@@ -122,7 +122,7 @@ function _isWebStorageSupported(): boolean {
122122
* @param {?Object=} global The optional global scope.
123123
* @return {boolean} Whether current environment is a worker.
124124
*/
125-
function _isWorker(): boolean {
125+
export function _isWorker(): boolean {
126126
// WorkerGlobalScope only defined in worker environment.
127127
return (
128128
typeof global !== 'undefined' &&

0 commit comments

Comments
 (0)