From 4600e96dc0dd3c7245d97970b62e7fae8a404cd1 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sun, 6 Sep 2015 20:19:53 +0200 Subject: [PATCH] firebase can be enabled through config --- config.json.example | 3 ++- website/src/libs/firebase.js | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/config.json.example b/config.json.example index 3b79e77aab..161e8a7b1b 100644 --- a/config.json.example +++ b/config.json.example @@ -72,6 +72,7 @@ }, "FIREBASE": { "APP": "app-name", - "SECRET": "secret" + "SECRET": "secret", + "ENABLED": "false" } } diff --git a/website/src/libs/firebase.js b/website/src/libs/firebase.js index 6721bae0a9..84a90b22d4 100644 --- a/website/src/libs/firebase.js +++ b/website/src/libs/firebase.js @@ -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)