Skip to content

Add platform specific persistence validation to auth-compat-exp #3593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages-exp/auth-compat-exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { version } from './package.json';
import { Auth } from './src/auth';
import { RecaptchaVerifier } from './src/recaptcha_verifier';
import { EmailAuthProvider } from './src/email_auth_provider';
import { Persistence } from './src/persistence';

const AUTH_TYPE = 'auth';

Expand Down Expand Up @@ -68,11 +69,7 @@ function registerAuth(instance: _FirebaseNamespace): void {
RecaptchaVerifier,
TwitterAuthProvider: impl.TwitterAuthProvider,
Auth: {
Persistence: {
LOCAL: 'LOCAL',
NONE: 'NONE',
SESSION: 'SESSION'
}
Persistence
}
// 'AuthCredential': fireauth.AuthCredential,
// 'Error': fireauth.AuthError
Expand Down
19 changes: 14 additions & 5 deletions packages-exp/auth-compat-exp/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import * as impl from '@firebase/auth-exp/internal';
import * as compat from '@firebase/auth-types';
import * as externs from '@firebase/auth-types-exp';
import '@firebase/installations';
import { Observer, Unsubscribe, ErrorFn } from '@firebase/util';
import {
Observer,
Unsubscribe,
ErrorFn,
isIndexedDBAvailable
} from '@firebase/util';
import { User } from './user';
import {
convertConfirmationResult,
convertCredential
} from './user_credential';
import { _isPopupRedirectSupported, _getClientPlatform } from './platform';
import { Persistence, _validatePersistenceArgument } from './persistence';

export class Auth extends impl.AuthImplCompat<User>
implements compat.FirebaseAuth {
Expand Down Expand Up @@ -164,12 +170,15 @@ export class Auth extends impl.AuthImplCompat<User>
auth: externs.Auth,
persistenceCompat: string
): externs.Persistence {
_validatePersistenceArgument(auth, persistence);
switch (persistenceCompat) {
case 'LOCAL':
return impl.browserLocalPersistence;
case 'SESSION':
case Persistence.SESSION:
return impl.browserSessionPersistence;
case 'NONE':
case Persistence.LOCAL:
return isIndexedDBAvailable()
? impl.indexedDBLocalPersistence
: impl.browserLocalPersistence;
case Persistence.NONE:
return impl.inMemoryPersistence;
default:
return impl.fail(auth.name, impl.AuthErrorCode.ARGUMENT_ERROR);
Expand Down
78 changes: 78 additions & 0 deletions packages-exp/auth-compat-exp/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @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 { assertFn, AuthErrorCode } from '@firebase/auth-exp/internal';
import * as externs from '@firebase/auth-types-exp';
import { isIndexedDBAvailable, isNode, isReactNative } from '@firebase/util';
import { _isWebStorageSupported, _isWorker } from './platform';

export const Persistence = {
LOCAL: 'LOCAL',
NONE: 'NONE',
SESSION: 'SESSION'
};

/**
* Validates that an argument is a valid persistence value. If an invalid type
* is specified, an error is thrown synchronously.
*/
export function _validatePersistenceArgument(
auth: externs.Auth,
persistence: string
): void {
assertFn(
Object.values(Persistence).includes(persistence),
auth.name,
AuthErrorCode.INVALID_PERSISTENCE
);
// Validate if the specified type is supported in the current environment.
if (isReactNative()) {
// This is only supported in a browser.
assertFn(
persistence !== Persistence.SESSION,
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
);
return;
}
if (isNode()) {
// Only none is supported in Node.js.
assertFn(
persistence === Persistence.NONE,
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
);
return;
}
if (_isWorker()) {
// In a worker environment, either LOCAL or NONE are supported.
// If indexedDB not supported and LOCAL provided, throw an error
assertFn(
persistence === Persistence.NONE ||
(persistence === Persistence.LOCAL && isIndexedDBAvailable()),
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
);
return;
}
// This is restricted by what the browser supports.
assertFn(
persistence === Persistence.NONE || _isWebStorageSupported(),
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
);
}
4 changes: 2 additions & 2 deletions packages-exp/auth-compat-exp/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function _isLocalStorageNotSynchronized(ua: string = getUA()): boolean {
}

/** @return {boolean} Whether web storage is supported. */
function _isWebStorageSupported(): boolean {
export function _isWebStorageSupported(): boolean {
try {
const storage = self.localStorage;
const key = impl._generateEventId();
Expand Down Expand Up @@ -122,7 +122,7 @@ function _isWebStorageSupported(): boolean {
* @param {?Object=} global The optional global scope.
* @return {boolean} Whether current environment is a worker.
*/
function _isWorker(): boolean {
export function _isWorker(): boolean {
// WorkerGlobalScope only defined in worker environment.
return (
typeof global !== 'undefined' &&
Expand Down