Skip to content

Commit e0e4d29

Browse files
committed
Migrate auth to component platform (#2317)
* Mirgrate Auth to component framework * format auth * fixed indentations
1 parent 0fac707 commit e0e4d29

File tree

7 files changed

+115
-58
lines changed

7 files changed

+115
-58
lines changed

packages/app-types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Provider } from '@firebase/component';
2+
13
/**
24
* @license
35
* Copyright 2017 Google Inc.

packages/auth-interop-types/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @firebase/auth-interop-types
2+
3+
**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.**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
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+
export interface FirebaseAuthTokenData {
19+
accessToken: string;
20+
}
21+
22+
export interface FirebaseAuthInternal {
23+
getToken(refreshToken?: boolean): Promise<FirebaseAuthTokenData | null>;
24+
getUid(): string | null;
25+
addAuthTokenListener(fn: (token: string | null) => void): void;
26+
removeAuthTokenListener(fn: (token: string | null) => void): void;
27+
}
28+
29+
declare module '@firebase/component' {
30+
interface ComponentContainer {
31+
getProvider(name: 'auth-internal'): Provider<FirebaseAuthInternal>;
32+
}
33+
34+
interface Provider<T> {}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@firebase/auth-interop-types",
3+
"version": "0.1.0",
4+
"description": "@firebase/auth interop Types",
5+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
6+
"license": "Apache-2.0",
7+
"scripts": {
8+
"test": "tsc"
9+
},
10+
"files": [
11+
"index.d.ts"
12+
],
13+
"peerDependencies": {
14+
"@firebase/app-types": "0.x",
15+
"@firebase/util": "0.x"
16+
},
17+
"repository": {
18+
"directory": "packages/auth-types",
19+
"type": "git",
20+
"url": "https://github.com/firebase/firebase-js-sdk.git"
21+
},
22+
"bugs": {
23+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
24+
},
25+
"devDependencies": {
26+
"typescript": "3.6.4"
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../config/tsconfig.base.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"exclude": [
7+
"dist/**/*"
8+
]
9+
}

packages/auth/src/exports_auth.js

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -618,27 +618,10 @@ fireauth.exportlib.exportFunction(
618618

619619
(function() {
620620
if (typeof firebase === 'undefined' || !firebase.INTERNAL ||
621-
!firebase.INTERNAL.registerService) {
621+
!firebase.INTERNAL.registerComponent) {
622622
throw new Error('Cannot find the firebase namespace; be sure to include ' +
623623
'firebase-app.js before this library.');
624624
} else {
625-
/** @type {!firebase.ServiceFactory} */
626-
var factory = function(app, extendApp) {
627-
var auth = new fireauth.Auth(app);
628-
extendApp({
629-
'INTERNAL': {
630-
// Extend app.INTERNAL.getUid.
631-
'getUid': goog.bind(auth.getUid, auth),
632-
'getToken': goog.bind(auth.getIdTokenInternal, auth),
633-
'addAuthTokenListener':
634-
goog.bind(auth.addAuthTokenListenerInternal, auth),
635-
'removeAuthTokenListener':
636-
goog.bind(auth.removeAuthTokenListenerInternal, auth)
637-
}
638-
});
639-
return auth;
640-
};
641-
642625
var namespace = {
643626
// Exports firebase.auth.ActionCodeInfo.Operation.
644627
'ActionCodeInfo': {
@@ -687,34 +670,43 @@ fireauth.exportlib.exportFunction(
687670
fireauth.exportlib.exportFunction(namespace,
688671
'ActionCodeURL', fireauth.ActionCodeURL, []);
689672

690-
// Register Auth service with firebase.App.
691-
firebase.INTERNAL.registerService(
692-
fireauth.exportlib.AUTH_TYPE,
693-
factory,
694-
namespace,
695-
// Initialize Auth when an App is created, so that tokens and Auth state
696-
// listeners are available.
697-
function (event, app) {
698-
if (event === 'create') {
699-
try {
700-
app[fireauth.exportlib.AUTH_TYPE]();
701-
} catch (e) {
702-
// This is a silent operation in the background. If the auth
703-
// initialization fails, it should not cause a fatal error.
704-
// Instead when the developer tries to initialize again manually,
705-
// the error will be thrown.
706-
// One specific use case here is the initialization for the nodejs
707-
// client when no API key is provided. This is commonly used
708-
// for unauthenticated database access.
709-
}
710-
}
711-
}
712-
);
713-
673+
// Create auth components to register with firebase
674+
const authComponent = { // provides Auth public APIs
675+
'name': fireauth.exportlib.AUTH_TYPE,
676+
'instanceFactory': function(container) {
677+
var app = container['getProvider']('app')['getImmediate']();
678+
return new fireauth.Auth(app);
679+
},
680+
'multipleInstances': false,
681+
'serviceProps': namespace,
682+
'instantiationMode': 'LAZY',
683+
'type': 'PUBLIC'
684+
};
685+
686+
const authInteropComponent = { // provides Auth internal APIs
687+
'name': 'auth-internal',
688+
'instanceFactory': function(container) {
689+
var auth = container['getProvider'](fireauth.exportlib.AUTH_TYPE)['getImmediate']();
690+
return {
691+
'getUid': goog.bind(auth.getUid, auth),
692+
'getToken': goog.bind(auth.getIdTokenInternal, auth),
693+
'addAuthTokenListener':
694+
goog.bind(auth.addAuthTokenListenerInternal, auth),
695+
'removeAuthTokenListener':
696+
goog.bind(auth.removeAuthTokenListenerInternal, auth)
697+
};
698+
},
699+
'multipleInstances': false,
700+
'instantiationMode': 'LAZY',
701+
'type': 'PRIVATE'
702+
};
703+
704+
firebase.INTERNAL.registerComponent(authComponent);
705+
firebase.INTERNAL.registerComponent(authInteropComponent);
714706

715707
// Expose User as firebase.User.
716708
firebase.INTERNAL.extendNamespace({
717709
'User': fireauth.AuthUser
718710
});
719711
}
720-
})();
712+
})();

packages/firebase/externs/firebase-app-internal-externs.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,9 @@
2020
*/
2121

2222
/**
23-
* @param {string} name Service name
24-
* @param {!firebase.ServiceFactory} createService
25-
* @param {Object=} serviceProperties
26-
* @param {(function(string, !firebase.app.App): void)=} appHook
27-
* @param {boolean=} allowMultipleInstances Whether the service registered
28-
* supports multiple instances on the same app.
29-
* @return {firebase.ServiceNamespace}
30-
*/
31-
firebase.INTERNAL.registerService = function(
32-
name,
33-
createService,
34-
serviceProperties,
35-
appHook,
36-
allowMultipleInstances
37-
) {};
23+
* @param {!firebase.FirebaseComponent}
24+
*/
25+
firebase.INTERNAL.registerComponent = function(component) {};
3826

3927
/** @param {!Object} props */
4028
firebase.INTERNAL.extendNamespace = function(props) {};

0 commit comments

Comments
 (0)