Skip to content

Register auth with the app component. Add the getAuth() methods for each platform #3637

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 11 commits into from
Aug 24, 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
6 changes: 3 additions & 3 deletions packages-exp/auth-compat-exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
*/

import firebase from '@firebase/app';
import * as impl from '@firebase/auth-exp/internal';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import * as impl from '@firebase/auth-exp/internal';
import * as externs from '@firebase/auth-types-exp';
import {
Component,
ComponentType,
InstantiationMode
} from '@firebase/component';
import '@firebase/installations';

import { version } from './package.json';
import { Auth } from './src/auth';
import { RecaptchaVerifier } from './src/recaptcha_verifier';
import { Persistence } from './src/persistence';
import { RecaptchaVerifier } from './src/recaptcha_verifier';

const AUTH_TYPE = 'auth';

Expand Down
16 changes: 9 additions & 7 deletions packages-exp/auth-compat-exp/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@
* limitations under the License.
*/

import '@firebase/installations';

import { FirebaseApp } from '@firebase/app-types';
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,
isIndexedDBAvailable
isIndexedDBAvailable,
Observer,
Unsubscribe
} from '@firebase/util';

import { _validatePersistenceArgument, Persistence } from './persistence';
import { _getClientPlatform, _isPopupRedirectSupported } from './platform';
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 All @@ -59,7 +61,7 @@ export class Auth extends impl.AuthImplCompat<User>
sdkClientVersion: impl._getClientVersion(_getClientPlatform())
};

super(app.name, config, User);
super(app, config, User);
this.app = app;

// This promise is intended to float; auth initialization happens in the
Expand Down
6 changes: 2 additions & 4 deletions packages-exp/auth-exp/demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
fetchSignInMethodsForEmail,
indexedDBLocalPersistence,
initializeAuth,
getAuth,
inMemoryPersistence,
isSignInWithEmailLink,
linkWithCredential,
Expand Down Expand Up @@ -1640,10 +1641,7 @@ function onApplyAuthSettingsChange() {
function initApp() {
log('Initializing app...');
app = initializeApp(config);
auth = initializeAuth(app, {
persistence: browserSessionPersistence,
popupRedirectResolver: browserPopupRedirectResolver
});
auth = getAuth(app);

tempApp = initializeApp(
{
Expand Down
7 changes: 3 additions & 4 deletions packages-exp/auth-exp/demo/src/worker/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
* mode.
*/
import { initializeApp } from '@firebase/app-exp';
import { initializeAuth, indexedDBLocalPersistence } from '@firebase/auth-exp';
import { getAuth } from '@firebase/auth-exp';
import { User } from '@firebase/auth-types-exp';

import { config } from '../config';

declare let self: ServiceWorkerGlobalScope;
Expand All @@ -38,9 +39,7 @@ const urlsToCache = [

// Initialize the Firebase app in the service worker.
const app = initializeApp(config);
const auth = initializeAuth(app, {
persistence: indexedDBLocalPersistence
});
const auth = getAuth(app);

/**
* Returns a promise that resolves with an ID token if available.
Expand Down
7 changes: 2 additions & 5 deletions packages-exp/auth-exp/demo/src/worker/web-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import { initializeApp } from '@firebase/app-exp';
import {
createUserWithEmailAndPassword,
fetchSignInMethodsForEmail,
getAuth,
GoogleAuthProvider,
initializeAuth,
inMemoryPersistence,
signInAnonymously,
signInWithCredential,
signInWithEmailAndPassword,
Expand All @@ -37,9 +36,7 @@ import { config } from '../config';

// Initialize the Firebase app in the web worker.
const app = initializeApp(config);
const auth = initializeAuth(app, {
persistence: inMemoryPersistence
});
const auth = getAuth(app);

/**
* Returns a promise that resolves with an ID token if available.
Expand Down
18 changes: 10 additions & 8 deletions packages-exp/auth-exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

import * as fetchImpl from 'node-fetch';

import { registerVersion } from '@firebase/app-exp';
import { name, version } from './package.json';
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
import { ClientPlatform } from './src/core/util/version';
import { FirebaseApp } from '@firebase/app-types-exp';
import { Auth } from '@firebase/auth-types-exp';

import { initializeAuth } from './src';
import { registerAuth } from './src/core/auth/register';
import { FetchProvider } from './src/core/util/fetch_provider';
import { ClientPlatform } from './src/core/util/version';

// Initialize the fetch polyfill, the types are slightly off so just cast and hope for the best
FetchProvider.initialize(
Expand All @@ -40,8 +42,8 @@ FetchProvider.initialize(
// Core functionality shared by all clients
export * from './src';

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.NODE
);
export function getAuth(app?: FirebaseApp): Auth {
return initializeAuth(app);
}

registerVersion(name, version, 'node');
registerAuth(ClientPlatform.NODE);
21 changes: 13 additions & 8 deletions packages-exp/auth-exp/index.rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
* just use index.ts
*/

import { registerVersion } from '@firebase/app-exp';
import { AsyncStorage } from 'react-native';
import { name, version } from './package.json';
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
import { getReactNativePersistence } from './src/platform_react_native/persistence/react_native';

import { FirebaseApp } from '@firebase/app-types-exp';
import { Auth } from '@firebase/auth-types-exp';

import { initializeAuth } from './src';
import { registerAuth } from './src/core/auth/register';
import { ClientPlatform } from './src/core/util/version';
import { getReactNativePersistence } from './src/platform_react_native/persistence/react_native';

// Core functionality shared by all clients
export * from './src';
Expand All @@ -36,8 +39,10 @@ export const reactNativeLocalPersistence = getReactNativePersistence(
AsyncStorage
);

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.REACT_NATIVE
);
export function getAuth(app?: FirebaseApp): Auth {
return initializeAuth(app, {
persistence: reactNativeLocalPersistence
});
}

registerVersion(name, version, 'rn');
registerAuth(ClientPlatform.REACT_NATIVE);
22 changes: 15 additions & 7 deletions packages-exp/auth-exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
* limitations under the License.
*/

import { registerVersion } from '@firebase/app-exp';
import { name, version } from './package.json';
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
import { FirebaseApp } from '@firebase/app-types-exp';
import { Auth } from '@firebase/auth-types-exp';

import { initializeAuth } from './src';
import { registerAuth } from './src/core/auth/register';
import { ClientPlatform } from './src/core/util/version';
import { browserLocalPersistence } from './src/platform_browser/persistence/browser';
import { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db';
import { browserPopupRedirectResolver } from './src/platform_browser/popup_redirect';

// Core functionality shared by all clients
export * from './src';
Expand Down Expand Up @@ -60,8 +65,11 @@ export { browserPopupRedirectResolver } from './src/platform_browser/popup_redir
// MFA
export { PhoneMultiFactorGenerator } from './src/platform_browser/mfa/assertions/phone';

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.BROWSER
);
export function getAuth(app?: FirebaseApp): Auth {
return initializeAuth(app, {
popupRedirectResolver: browserPopupRedirectResolver,
persistence: [indexedDBLocalPersistence, browserLocalPersistence]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we do the same style of fallback for workers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "fallback" is doing different things. Here, it's saying "check these persistences in this order," whereas for workers, it's literally checking to see if it's available (if you tried to pass indexedDB into it when it wasn't available, the instantiation would just error out)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't auth fallback to memory persistence when indexedDB is not available. Why is it different for workers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it would error. It probably shouldn't but we need to clarify that separately. For now I'd like to move ahead and deal with that issue in a separate PR. I'm tracking this in b/165376704

});
}

registerVersion(name, version);
registerAuth(ClientPlatform.BROWSER);
37 changes: 30 additions & 7 deletions packages-exp/auth-exp/index.webworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,39 @@
* limitations under the License.
*/

import { registerVersion } from '@firebase/app-exp';
import { name, version } from './package.json';
import { _initializeAuthForClientPlatform } from './src/core/auth/auth_impl';
import { _getProvider, getApp } from '@firebase/app-exp';
import { Auth } from '@firebase/auth-types-exp';

import { AuthImpl } from './src/core/auth/auth_impl';
import { _initializeAuthInstance } from './src/core/auth/initialize';
import { _AUTH_COMPONENT_NAME, registerAuth } from './src/core/auth/register';
import { Persistence } from './src/core/persistence';
import { _getInstance } from './src/core/util/instantiator';
import { ClientPlatform } from './src/core/util/version';
import { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db';

// Core functionality shared by all clients
export * from './src';

export const initializeAuth = _initializeAuthForClientPlatform(
ClientPlatform.WORKER
);
registerAuth(ClientPlatform.WORKER);

export function getAuth(app = getApp()): Auth {
// Unlike the other environments, we need to explicitly check if indexedDb is
// available. That means doing the whole rigamarole
const auth = _getProvider(
app,
_AUTH_COMPONENT_NAME
).getImmediate() as AuthImpl;

// This promise is intended to float; auth initialization happens in the
// background, meanwhile the auth object may be used by the app.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
_getInstance<Persistence>(indexedDBLocalPersistence)
.isAvailable()
.then(avail => {
const deps = avail ? { persistence: indexedDBLocalPersistence } : {};
_initializeAuthInstance(auth, deps);
});

registerVersion(name, version, 'webworker');
return auth;
}
3 changes: 2 additions & 1 deletion packages-exp/auth-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
"peerDependencies": {
"@firebase/app-exp": "0.x",
"@firebase/app-types-exp": "0.x",
"@firebase/auth-types-exp": "0.x",
"@firebase/auth-exp": "0.x",
"@firebase/auth-types-exp": "0.x",
"@firebase/util": "0.x"
},
"dependencies": {
"@firebase/component": "0.1.17",
"@firebase/logger": "^0.2.6",
"@firebase/util": "^0.3.0",
"node-fetch": "2.6.0",
Expand Down
37 changes: 24 additions & 13 deletions packages-exp/auth-exp/src/core/auth/auth_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,29 @@
* limitations under the License.
*/

import { FirebaseApp } from '@firebase/app-types-exp';
import { FirebaseError } from '@firebase/util';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

import { FirebaseApp } from '@firebase/app-types-exp';
import { FirebaseError } from '@firebase/util';

import { testUser } from '../../../test/helpers/mock_auth';
import { Auth } from '../../model/auth';
import { User } from '../../model/user';
import { Persistence } from '../persistence';
import { inMemoryPersistence } from '../persistence/in_memory';
import { _getInstance } from '../util/instantiator';
import * as navigator from '../util/navigator';
import { ClientPlatform } from '../util/version';
import { _castAuth, _initializeAuthForClientPlatform } from './auth_impl';
import { Auth } from '../../model/auth';
import {
_castAuth,
AuthImpl,
DEFAULT_API_HOST,
DEFAULT_API_SCHEME,
DEFAULT_TOKEN_API_HOST
} from './auth_impl';
import { _initializeAuthInstance } from './initialize';

use(sinonChai);
use(chaiAsPromised);
Expand All @@ -43,19 +51,22 @@ const FAKE_APP: FirebaseApp = {
automaticDataCollectionEnabled: false
};

const initializeAuth = _initializeAuthForClientPlatform(ClientPlatform.BROWSER);

describe('core/auth/auth_impl', () => {
let auth: Auth;
let persistenceStub: sinon.SinonStubbedInstance<Persistence>;

beforeEach(() => {
beforeEach(async () => {
persistenceStub = sinon.stub(_getInstance(inMemoryPersistence));
auth = _castAuth(
initializeAuth(FAKE_APP, {
persistence: inMemoryPersistence
})
);
const authImpl = new AuthImpl(FAKE_APP, {
apiKey: FAKE_APP.options.apiKey!,
apiHost: DEFAULT_API_HOST,
apiScheme: DEFAULT_API_SCHEME,
tokenApiHost: DEFAULT_TOKEN_API_HOST,
sdkClientVersion: 'v'
});

_initializeAuthInstance(authImpl, { persistence: inMemoryPersistence });
auth = authImpl;
});

afterEach(sinon.restore);
Expand Down
Loading