diff --git a/packages/app-types/index.d.ts b/packages/app-types/index.d.ts index 10b89b83bb9..eab147e16c8 100644 --- a/packages/app-types/index.d.ts +++ b/packages/app-types/index.d.ts @@ -1,3 +1,5 @@ +import { Provider } from '@firebase/component'; + /** * @license * Copyright 2017 Google Inc. diff --git a/packages/auth-interop-types/README.md b/packages/auth-interop-types/README.md new file mode 100644 index 00000000000..67ed6389e04 --- /dev/null +++ b/packages/auth-interop-types/README.md @@ -0,0 +1,3 @@ +# @firebase/auth-interop-types + +**This package is not intended for direct usage, and should only be used via the officially supported [firebase](https://www.npmjs.com/package/firebase) package.** diff --git a/packages/auth-interop-types/index.d.ts b/packages/auth-interop-types/index.d.ts new file mode 100644 index 00000000000..ef107576c0f --- /dev/null +++ b/packages/auth-interop-types/index.d.ts @@ -0,0 +1,35 @@ +/** + * @license + * Copyright 2019 Google Inc. + * + * 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. + */ + +export interface FirebaseAuthTokenData { + accessToken: string; +} + +export interface FirebaseAuthInternal { + getToken(refreshToken?: boolean): Promise; + getUid(): string | null; + addAuthTokenListener(fn: (token: string | null) => void): void; + removeAuthTokenListener(fn: (token: string | null) => void): void; +} + +declare module '@firebase/component' { + interface ComponentContainer { + getProvider(name: 'auth-internal'): Provider; + } + + interface Provider {} +} diff --git a/packages/auth-interop-types/package.json b/packages/auth-interop-types/package.json new file mode 100644 index 00000000000..ee52ca167f6 --- /dev/null +++ b/packages/auth-interop-types/package.json @@ -0,0 +1,28 @@ +{ + "name": "@firebase/auth-interop-types", + "version": "0.1.0", + "description": "@firebase/auth interop Types", + "author": "Firebase (https://firebase.google.com/)", + "license": "Apache-2.0", + "scripts": { + "test": "tsc" + }, + "files": [ + "index.d.ts" + ], + "peerDependencies": { + "@firebase/app-types": "0.x", + "@firebase/util": "0.x" + }, + "repository": { + "directory": "packages/auth-types", + "type": "git", + "url": "https://github.com/firebase/firebase-js-sdk.git" + }, + "bugs": { + "url": "https://github.com/firebase/firebase-js-sdk/issues" + }, + "devDependencies": { + "typescript": "3.6.4" + } +} diff --git a/packages/auth-interop-types/tsconfig.json b/packages/auth-interop-types/tsconfig.json new file mode 100644 index 00000000000..9a785433d90 --- /dev/null +++ b/packages/auth-interop-types/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../config/tsconfig.base.json", + "compilerOptions": { + "noEmit": true + }, + "exclude": [ + "dist/**/*" + ] +} diff --git a/packages/auth/src/exports_auth.js b/packages/auth/src/exports_auth.js index 97f5b6b1ae4..acd2dba6e18 100644 --- a/packages/auth/src/exports_auth.js +++ b/packages/auth/src/exports_auth.js @@ -618,27 +618,10 @@ fireauth.exportlib.exportFunction( (function() { if (typeof firebase === 'undefined' || !firebase.INTERNAL || - !firebase.INTERNAL.registerService) { + !firebase.INTERNAL.registerComponent) { throw new Error('Cannot find the firebase namespace; be sure to include ' + 'firebase-app.js before this library.'); } else { - /** @type {!firebase.ServiceFactory} */ - var factory = function(app, extendApp) { - var auth = new fireauth.Auth(app); - extendApp({ - 'INTERNAL': { - // Extend app.INTERNAL.getUid. - 'getUid': goog.bind(auth.getUid, auth), - 'getToken': goog.bind(auth.getIdTokenInternal, auth), - 'addAuthTokenListener': - goog.bind(auth.addAuthTokenListenerInternal, auth), - 'removeAuthTokenListener': - goog.bind(auth.removeAuthTokenListenerInternal, auth) - } - }); - return auth; - }; - var namespace = { // Exports firebase.auth.ActionCodeInfo.Operation. 'ActionCodeInfo': { @@ -687,34 +670,43 @@ fireauth.exportlib.exportFunction( fireauth.exportlib.exportFunction(namespace, 'ActionCodeURL', fireauth.ActionCodeURL, []); - // Register Auth service with firebase.App. - firebase.INTERNAL.registerService( - fireauth.exportlib.AUTH_TYPE, - factory, - namespace, - // Initialize Auth when an App is created, so that tokens and Auth state - // listeners are available. - function (event, app) { - if (event === 'create') { - try { - app[fireauth.exportlib.AUTH_TYPE](); - } catch (e) { - // This is a silent operation in the background. If the auth - // initialization fails, it should not cause a fatal error. - // Instead when the developer tries to initialize again manually, - // the error will be thrown. - // One specific use case here is the initialization for the nodejs - // client when no API key is provided. This is commonly used - // for unauthenticated database access. - } - } - } - ); - + // Create auth components to register with firebase + const authComponent = { // provides Auth public APIs + 'name': fireauth.exportlib.AUTH_TYPE, + 'instanceFactory': function(container) { + var app = container['getProvider']('app')['getImmediate'](); + return new fireauth.Auth(app); + }, + 'multipleInstances': false, + 'serviceProps': namespace, + 'instantiationMode': 'LAZY', + 'type': 'PUBLIC' + }; + + const authInteropComponent = { // provides Auth internal APIs + 'name': 'auth-internal', + 'instanceFactory': function(container) { + var auth = container['getProvider'](fireauth.exportlib.AUTH_TYPE)['getImmediate'](); + return { + 'getUid': goog.bind(auth.getUid, auth), + 'getToken': goog.bind(auth.getIdTokenInternal, auth), + 'addAuthTokenListener': + goog.bind(auth.addAuthTokenListenerInternal, auth), + 'removeAuthTokenListener': + goog.bind(auth.removeAuthTokenListenerInternal, auth) + }; + }, + 'multipleInstances': false, + 'instantiationMode': 'LAZY', + 'type': 'PRIVATE' + }; + + firebase.INTERNAL.registerComponent(authComponent); + firebase.INTERNAL.registerComponent(authInteropComponent); // Expose User as firebase.User. firebase.INTERNAL.extendNamespace({ 'User': fireauth.AuthUser }); } -})(); +})(); \ No newline at end of file diff --git a/packages/firebase/externs/firebase-app-internal-externs.js b/packages/firebase/externs/firebase-app-internal-externs.js index fc1ceba92c1..c900b2d6b33 100644 --- a/packages/firebase/externs/firebase-app-internal-externs.js +++ b/packages/firebase/externs/firebase-app-internal-externs.js @@ -20,21 +20,9 @@ */ /** - * @param {string} name Service name - * @param {!firebase.ServiceFactory} createService - * @param {Object=} serviceProperties - * @param {(function(string, !firebase.app.App): void)=} appHook - * @param {boolean=} allowMultipleInstances Whether the service registered - * supports multiple instances on the same app. - * @return {firebase.ServiceNamespace} - */ -firebase.INTERNAL.registerService = function( - name, - createService, - serviceProperties, - appHook, - allowMultipleInstances -) {}; + * @param {!firebase.FirebaseComponent} + */ +firebase.INTERNAL.registerComponent = function(component) {}; /** @param {!Object} props */ firebase.INTERNAL.extendNamespace = function(props) {};