Skip to content

Commit

Permalink
firebase can be enabled through config
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Sep 6, 2015
1 parent 607ddbb commit 4600e96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
},
"FIREBASE": {
"APP": "app-name",
"SECRET": "secret"
"SECRET": "secret",
"ENABLED": "false"
}
}
19 changes: 11 additions & 8 deletions website/src/libs/firebase.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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)
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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)
Expand Down

0 comments on commit 4600e96

Please sign in to comment.