firebase can be enabled through config

This commit is contained in:
Matteo Pagliazzi
2015-09-06 20:19:53 +02:00
parent 607ddbb1f2
commit 4600e96dc0
2 changed files with 13 additions and 9 deletions
+2 -1
View File
@@ -72,6 +72,7 @@
},
"FIREBASE": {
"APP": "app-name",
"SECRET": "secret"
"SECRET": "secret",
"ENABLED": "false"
}
}
+11 -8
View File
@@ -1,13 +1,16 @@
var Firebase = require('firebase');
var nconf = require('nconf');
var isProd = false //nconf.get('NODE_ENV') === 'production';
var firebaseRef;
var isProd = nconf.get('NODE_ENV') === 'production';
var firebaseConfig = nconf.get('FIREBASE');
var firebaseRef;
var isFirebaseEnabled = (nconf.get('NODE_ENV') === 'production') && (firebaseConfig.ENABLED === 'true');
// Setup
if(isProd){
if(isFirebaseEnabled){
firebaseRef = new Firebase('https://' + firebaseConfig.APP + '.firebaseio.com');
// TODO what happens if an op is sent before client is authenticated?
firebaseRef.authWithCustomToken(firebaseConfig.SECRET, function(err, authData){
// TODO it's ok to kill the server here? what if FB is offline?
if(err) throw new Error('Impossible to authenticate Firebase');
@@ -17,7 +20,7 @@ if(isProd){
var api = module.exports = {};
api.updateGroupData = function(group){
if(!isProd) return;
if(!isFirebaseEnabled) return;
// TODO is throw ok? we don't have callbacks
if(!group) throw new Error('group is required.');
// Return in case of tavern (comparison working because we use string for _id)
@@ -30,7 +33,7 @@ api.updateGroupData = function(group){
};
api.addUserToGroup = function(groupId, userId){
if(!isProd) return;
if(!isFirebaseEnabled) return;
if(!userId || !groupId) throw new Error('groupId, userId are required.');
if(groupId === 'habitrpg') return;
@@ -42,7 +45,7 @@ api.addUserToGroup = function(groupId, userId){
};
api.removeUserFromGroup = function(groupId, userId){
if(!isProd) return;
if(!isFirebaseEnabled) return;
if(!userId || !groupId) throw new Error('groupId, userId are required.');
if(groupId === 'habitrpg') return;
@@ -54,7 +57,7 @@ api.removeUserFromGroup = function(groupId, userId){
};
api.deleteGroup = function(groupId){
if(!isProd) return;
if(!isFirebaseEnabled) return;
if(!groupId) throw new Error('groupId is required.');
if(groupId === 'habitrpg') return;
@@ -70,7 +73,7 @@ api.deleteGroup = function(groupId){
// FIXME not really necessary as long as we only store room data,
// as empty objects are automatically deleted
api.deleteUser = function(userId){
if(!isProd) return;
if(!isFirebaseEnabled) return;
if(!userId) throw new Error('userId is required.');
firebaseRef.child('users/' + userId)