Skip to content

Commit

Permalink
Use empty auth token provider when talking to Emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jun 17, 2020
1 parent c4b7595 commit 9d69fd9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
25 changes: 21 additions & 4 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ import { Provider } from '@firebase/component';
import { log, warn } from './util/util';
import { FirebaseApp } from '@firebase/app-types';

export interface AuthTokenProvider {
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData>;
addTokenChangeListener(listener: (token: string | null) => void): void;
removeTokenChangeListener(listener: (token: string | null) => void): void;
notifyForInvalidToken(): void;
}

/**
* Abstraction around FirebaseApp's token fetching capabilities.
*/
export class AuthTokenProvider {
export class FirebaseAuthTokenProvider implements AuthTokenProvider {
private auth_: FirebaseAuthInternal | null = null;
constructor(
private app_: FirebaseApp,
Expand Down Expand Up @@ -60,7 +67,7 @@ export class AuthTokenProvider {
});
}

addTokenChangeListener(listener: (token: string | null) => void) {
addTokenChangeListener(listener: (token: string | null) => void): void {
// TODO: We might want to wrap the listener and call it with no args to
// avoid a leaky abstraction, but that makes removing the listener harder.
if (this.auth_) {
Expand All @@ -73,13 +80,13 @@ export class AuthTokenProvider {
}
}

removeTokenChangeListener(listener: (token: string | null) => void) {
removeTokenChangeListener(listener: (token: string | null) => void): void {
this.authProvider_
.get()
.then(auth => auth.removeAuthTokenListener(listener));
}

notifyForInvalidToken() {
notifyForInvalidToken(): void {
let errorMessage =
'Provided authentication credentials for the app named "' +
this.app_.name +
Expand All @@ -104,3 +111,13 @@ export class AuthTokenProvider {
warn(errorMessage);
}
}

/* Auth token provider used to connect to the Emulator. */
export class EmptyAuthTokenProvider implements AuthTokenProvider {
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
return Promise.resolve({ accessToken: '' });
}
addTokenChangeListener(listener: (token: string | null) => void): void {}
removeTokenChangeListener(listener: (token: string | null) => void): void {}
notifyForInvalidToken(): void {}
}
4 changes: 1 addition & 3 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ export class Repo {
public repoInfo_: RepoInfo,
forceRestClient: boolean,
public app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>
authTokenProvider: AuthTokenProvider
) {
const authTokenProvider = new AuthTokenProvider(app, authProvider);

this.stats_ = StatsManager.getCollection(repoInfo_);

if (forceRestClient || beingCrawled()) {
Expand Down
16 changes: 13 additions & 3 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { Database } from '../api/Database';
import { RepoInfo } from './RepoInfo';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import {
AuthTokenProvider,
EmptyAuthTokenProvider,
FirebaseAuthTokenProvider
} from './AuthTokenProvider';

/** @const {string} */
const DATABASE_URL_OPTION = 'databaseURL';
Expand Down Expand Up @@ -108,6 +113,8 @@ export class RepoManager {
let parsedUrl = parseRepoInfo(dbUrl);
let repoInfo = parsedUrl.repoInfo;

let authTokenProvider: AuthTokenProvider;

let dbEmulatorHost: string | undefined = undefined;
if (typeof process !== 'undefined') {
dbEmulatorHost = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
Expand All @@ -116,6 +123,9 @@ export class RepoManager {
dbUrl = `http://${dbEmulatorHost}?ns=${repoInfo.namespace}`;
parsedUrl = parseRepoInfo(dbUrl);
repoInfo = parsedUrl.repoInfo;
authTokenProvider = new EmptyAuthTokenProvider();
} else {
authTokenProvider = new FirebaseAuthTokenProvider(app, authProvider);
}

validateUrl('Invalid Firebase Database URL', 1, parsedUrl);
Expand All @@ -126,7 +136,7 @@ export class RepoManager {
);
}

const repo = this.createRepo(repoInfo, app, authProvider);
const repo = this.createRepo(repoInfo, app, authTokenProvider);

return repo.database;
}
Expand Down Expand Up @@ -159,7 +169,7 @@ export class RepoManager {
createRepo(
repoInfo: RepoInfo,
app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>
authTokenProvider: AuthTokenProvider
): Repo {
let appRepos = safeGet(this.repos_, app.name);

Expand All @@ -174,7 +184,7 @@ export class RepoManager {
'Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.'
);
}
repo = new Repo(repoInfo, this.useRestClient_, app, authProvider);
repo = new Repo(repoInfo, this.useRestClient_, app, authTokenProvider);
appRepos[repoInfo.toURLString()] = repo;

return repo;
Expand Down

0 comments on commit 9d69fd9

Please sign in to comment.