Skip to content

Commit

Permalink
Cleanup + Sam's constants
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jul 8, 2020
1 parent 88e8f22 commit 84a9b29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
23 changes: 4 additions & 19 deletions packages/database/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,8 @@ const ServerValue = Database.ServerValue;
* @param app A valid FirebaseApp-like object
* @param url A valid Firebase databaseURL
* @param version custom version e.g. firebase-admin version
* @param thirdPartyAuth Whether to use third party auth for this client (`true`
* for NodeJS users that use the client with end user credentials, `false` for
* Firebase Admin)
*/
export function initStandalone(
app: FirebaseApp,
url: string,
version: string,
thirdPartyAuth = false
) {
export function initStandalone(app: FirebaseApp, url: string, version: string) {
/**
* This should allow the firebase-admin package to provide a custom version
* to the backend
Expand Down Expand Up @@ -89,12 +81,7 @@ export function initStandalone(
);

return {
instance: RepoManager.getInstance().databaseFromApp(
app,
authProvider,
url,
thirdPartyAuth
),
instance: RepoManager.getInstance().databaseFromApp(app, authProvider, url),
namespace: {
Reference,
Query,
Expand Down Expand Up @@ -125,8 +112,7 @@ export function registerDatabase(instance: FirebaseNamespace) {
return RepoManager.getInstance().databaseFromApp(
app,
authProvider,
url,
/* thirdPartyAuth= */ true
url
);
},
ComponentType.PUBLIC
Expand All @@ -150,8 +136,7 @@ export function registerDatabase(instance: FirebaseNamespace) {
instance.registerVersion(name, version, 'node');

if (isNodeSdk()) {
module.exports = (app: FirebaseApp, url: string, version: string) =>
initStandalone(app, url, version, /* thirdPartyAuth= */ true);
module.exports = Object.assign({}, namespace, { initStandalone });
}
}

Expand Down
17 changes: 14 additions & 3 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,22 @@ export class FirebaseAuthTokenProvider implements AuthTokenProvider {
}

/* Auth token provider that the Admin SDK uses to connect to the Emulator. */
export class EmptyAdminAuthTokenProvider implements AuthTokenProvider {
export class EmulatorAdminTokenProvider implements AuthTokenProvider {
private static EMULATOR_AUTH_TOKEN = 'owner';

getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return Promise.resolve({ accessToken: 'Bearer owner' });
return Promise.resolve({
accessToken: EmulatorAdminTokenProvider.EMULATOR_AUTH_TOKEN
});
}
addTokenChangeListener(listener: (token: string | null) => void): void {}

addTokenChangeListener(listener: (token: string | null) => void): void {
// Invoke the listener immediately to match the behavior in Firebase Auth
// (see packages/auth/src/auth.js#L1807)
listener(EmulatorAdminTokenProvider.EMULATOR_AUTH_TOKEN);
}

removeTokenChangeListener(listener: (token: string | null) => void): void {}

notifyForInvalidToken(): void {}
}
13 changes: 6 additions & 7 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { FirebaseApp } from '@firebase/app-types';
import { safeGet } from '@firebase/util';
import { safeGet, CONSTANTS } from '@firebase/util';
import { Repo } from './Repo';
import { fatal } from './util/util';
import { parseRepoInfo } from './util/libs/parser';
Expand All @@ -28,7 +28,7 @@ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import {
AuthTokenProvider,
EmptyAdminAuthTokenProvider,
EmulatorAdminTokenProvider,
FirebaseAuthTokenProvider
} from './AuthTokenProvider';

Expand Down Expand Up @@ -99,8 +99,7 @@ export class RepoManager {
databaseFromApp(
app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>,
url?: string,
thirdPartyAuth?: boolean
url?: string
): Database {
let dbUrl: string | undefined = url || app.options[DATABASE_URL_OPTION];
if (dbUrl === undefined) {
Expand All @@ -124,9 +123,9 @@ export class RepoManager {
dbUrl = `http://${dbEmulatorHost}?ns=${repoInfo.namespace}`;
parsedUrl = parseRepoInfo(dbUrl);
repoInfo = parsedUrl.repoInfo;
authTokenProvider = thirdPartyAuth
? new FirebaseAuthTokenProvider(app, authProvider)
: new EmptyAdminAuthTokenProvider();
authTokenProvider = CONSTANTS.NODE_ADMIN
? new EmulatorAdminTokenProvider()
: new FirebaseAuthTokenProvider(app, authProvider);
} else {
authTokenProvider = new FirebaseAuthTokenProvider(app, authProvider);
}
Expand Down

0 comments on commit 84a9b29

Please sign in to comment.