Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Another attempt
Browse files Browse the repository at this point in the history
schmidt-sebastian committed Jul 7, 2020
1 parent 9d69fd9 commit 88e8f22
Showing 3 changed files with 28 additions and 10 deletions.
23 changes: 19 additions & 4 deletions packages/database/index.node.ts
Original file line number Diff line number Diff line change
@@ -51,8 +51,16 @@ 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) {
export function initStandalone(
app: FirebaseApp,
url: string,
version: string,
thirdPartyAuth = false
) {
/**
* This should allow the firebase-admin package to provide a custom version
* to the backend
@@ -81,7 +89,12 @@ export function initStandalone(app: FirebaseApp, url: string, version: string) {
);

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

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

This comment has been minimized.

Copy link
@samtstern

samtstern Jul 7, 2020

Contributor

isNodeSdk() is defined as:

export function isNodeSdk(): boolean {
  return CONSTANTS.NODE_CLIENT === true || CONSTANTS.NODE_ADMIN === true;
}

I think we want thirdPartyAuth= !Constants.NODE_ADMIN unless I am misunderstanding.

This comment has been minimized.

Copy link
@samtstern

samtstern Jul 7, 2020

Contributor

Unless thirdPartyAuth just means "use any auth at all"? I'm reading it as "Firebase Auth".

initStandalone(app, url, version, /* thirdPartyAuth= */ true);
}
}

6 changes: 3 additions & 3 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
@@ -112,10 +112,10 @@ export class FirebaseAuthTokenProvider implements AuthTokenProvider {
}
}

/* Auth token provider used to connect to the Emulator. */
export class EmptyAuthTokenProvider implements AuthTokenProvider {
/* Auth token provider that the Admin SDK uses to connect to the Emulator. */
export class EmptyAdminAuthTokenProvider implements AuthTokenProvider {
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return Promise.resolve({ accessToken: '' });
return Promise.resolve({ accessToken: 'Bearer owner' });

This comment has been minimized.

Copy link
@samtstern

samtstern Jul 7, 2020

Contributor

I think this should just be owner and the Bearer part is added before all tokens:
https://github.com/firebase/firebase-js-sdk/blob/master/packages/testing/src/api/index.ts#L50

This may become Authorization: Bearer Bearer owner when made into a header.

This comment has been minimized.

Copy link
@schmidt-sebastian

schmidt-sebastian Jul 7, 2020

Author Contributor

FYI I am still working on this (and in the process of testing). Your comments probably saved me a bunch of time though.

}
addTokenChangeListener(listener: (token: string | null) => void): void {}
removeTokenChangeListener(listener: (token: string | null) => void): void {}
9 changes: 6 additions & 3 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import {
AuthTokenProvider,
EmptyAuthTokenProvider,
EmptyAdminAuthTokenProvider,
FirebaseAuthTokenProvider
} from './AuthTokenProvider';

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

0 comments on commit 88e8f22

Please sign in to comment.