Skip to content

Migrate auth to component platform #2317

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 3 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/app-types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Provider } from '@firebase/component';

/**
* @license
* Copyright 2017 Google Inc.
Expand Down
3 changes: 3 additions & 0 deletions packages/auth-interop-types/README.md
Original file line number Diff line number Diff line change
@@ -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.**
35 changes: 35 additions & 0 deletions packages/auth-interop-types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<FirebaseAuthTokenData | null>;
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<FirebaseAuthInternal>;
}

interface Provider<T> {}
}
28 changes: 28 additions & 0 deletions packages/auth-interop-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@firebase/auth-interop-types",
"version": "0.1.0",
"description": "@firebase/auth interop Types",
"author": "Firebase <[email protected]> (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"
}
}
9 changes: 9 additions & 0 deletions packages/auth-interop-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"noEmit": true
},
"exclude": [
"dist/**/*"
]
}
78 changes: 35 additions & 43 deletions packages/auth/src/exports_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -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
});
}
})();
})();
18 changes: 3 additions & 15 deletions packages/firebase/externs/firebase-app-internal-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {};
Expand Down