Skip to content

Commit 7a26d3e

Browse files
committed
omit access token exchange if talking to an emulator
1 parent 8271edf commit 7a26d3e

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

packages/database/src/core/PersistentConnection.ts

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { isMobileCordova, isReactNative, isNodeSdk } from '@firebase/util';
2828
import { ServerActions } from './ServerActions';
2929
import { AuthTokenProvider } from './AuthTokenProvider';
3030
import { RepoInfo } from './RepoInfo';
31+
import { FIREBASE_DATABASE_EMULATOR_HOST_VAR } from './RepoManager';
3132
import { Query } from '../api/Query';
3233
import { SDK_VERSION } from './version';
3334

@@ -41,6 +42,8 @@ const SERVER_KILL_INTERRUPT_REASON = 'server_kill';
4142
// If auth fails repeatedly, we'll assume something is wrong and log a warning / back off.
4243
const INVALID_AUTH_TOKEN_THRESHOLD = 3;
4344

45+
const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
46+
4447
interface ListenSpec {
4548
onComplete(s: string, p?: any): void;
4649

@@ -740,41 +743,58 @@ export class PersistentConnection extends ServerActions {
740743
const forceRefresh = this.forceTokenRefresh_;
741744
this.forceTokenRefresh_ = false;
742745

743-
// First fetch auth token, and establish connection after fetching the token was successful
744-
this.authTokenProvider_
745-
.getToken(forceRefresh)
746-
.then(function(result) {
747-
if (!canceled) {
748-
log('getToken() completed. Creating connection.');
749-
self.authToken_ = result && result.accessToken;
750-
connection = new Connection(
751-
connId,
752-
self.repoInfo_,
753-
onDataMessage,
754-
onReady,
755-
onDisconnect,
756-
/* onKill= */ function(reason) {
757-
warn(reason + ' (' + self.repoInfo_.toString() + ')');
758-
self.interrupt(SERVER_KILL_INTERRUPT_REASON);
759-
},
760-
lastSessionId
761-
);
762-
} else {
763-
log('getToken() completed but was canceled');
764-
}
765-
})
766-
.then(null, function(error) {
767-
self.log_('Failed to get token: ' + error);
768-
if (!canceled) {
769-
if (CONSTANTS.NODE_ADMIN) {
770-
// This may be a critical error for the Admin Node.js SDK, so log a warning.
771-
// But getToken() may also just have temporarily failed, so we still want to
772-
// continue retrying.
773-
warn(error);
746+
if (typeof process !== 'undefined' && process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR]) {
747+
log('Connecting to a database emulator, ommitting access token requests.');
748+
self.authToken_ = "owner"
749+
connection = new Connection(
750+
connId,
751+
self.repoInfo_,
752+
onDataMessage,
753+
onReady,
754+
onDisconnect,
755+
/* onKill= */ function(reason) {
756+
warn(reason + ' (' + self.repoInfo_.toString() + ')');
757+
self.interrupt(SERVER_KILL_INTERRUPT_REASON);
758+
},
759+
lastSessionId
760+
);
761+
} else {
762+
// First fetch auth token, and establish connection after fetching the token was successful
763+
this.authTokenProvider_
764+
.getToken(forceRefresh)
765+
.then(function(result) {
766+
if (!canceled) {
767+
log('getToken() completed. Creating connection.');
768+
self.authToken_ = result && result.accessToken;
769+
connection = new Connection(
770+
connId,
771+
self.repoInfo_,
772+
onDataMessage,
773+
onReady,
774+
onDisconnect,
775+
/* onKill= */ function(reason) {
776+
warn(reason + ' (' + self.repoInfo_.toString() + ')');
777+
self.interrupt(SERVER_KILL_INTERRUPT_REASON);
778+
},
779+
lastSessionId
780+
);
781+
} else {
782+
log('getToken() completed but was canceled');
774783
}
775-
closeFn();
776-
}
777-
});
784+
})
785+
.then(null, function(error) {
786+
self.log_('Failed to get token: ' + error);
787+
if (!canceled) {
788+
if (CONSTANTS.NODE_ADMIN) {
789+
// This may be a critical error for the Admin Node.js SDK, so log a warning.
790+
// But getToken() may also just have temporarily failed, so we still want to
791+
// continue retrying.
792+
warn(error);
793+
}
794+
closeFn();
795+
}
796+
});
797+
}
778798
}
779799
}
780800

packages/database/src/core/RepoManager.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ import { RepoInfo } from './RepoInfo';
2828
/** @const {string} */
2929
const DATABASE_URL_OPTION = 'databaseURL';
3030

31-
/**
32-
* This variable is also defined in the firebase node.js admin SDK. Before
33-
* modifying this definition, consult the definition in:
34-
*
35-
* https://github.com/firebase/firebase-admin-node
36-
*
37-
* and make sure the two are consistent.
38-
*/
39-
const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
31+
export const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
4032

4133
let _staticInstance: RepoManager;
4234

0 commit comments

Comments
 (0)