Skip to content

Commit

Permalink
Updated to allow multiple auth's to the same Firebase! See Issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Eldridge authored and Austin Eldridge committed Feb 22, 2016
1 parent 50a21af commit 9e3f768
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 56 deletions.
44 changes: 0 additions & 44 deletions firebase/firebase_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
.firebaseio.com/
</div>

<div id="node-config-input-firebaseurl-input-error-container" class="form-tips hidden">
<p id="node-config-input-firebaseurl-input-error" class="text-center" style="margin: 0;"><strong>Firebase <span id="fb-error-name"></span> already added!</strong></p>
</div>

<!-- Select type -->
<div class="form-row">
<label for="loginType-select"><i class="fa fa-shield"></i> Auth Type</label>
Expand Down Expand Up @@ -145,46 +141,6 @@
//TODO: Possible node-red bug? It looks like defaults don't get set automatically in config nodes?
$("#node-config-input-firebaseurl").val(this.firebaseurl)

if (!this.firebaseurl){
$("#node-config-input-firebaseurl").on('input', function(){
var val = $(this).val(); //check against $('#node-input-firebaseconfig')
var invalid = false;
$('#node-input-firebaseconfig option').each(function(idx){
//console.log($(this).val());
if ($(this).val() != "_ADD_"){
//TODO: This needs to be cleaned up (gets "pvm-alpha" from https://pvm-alpha.firebaseio.com)
var url = $(this).text();
var arr = url.split("//");
var arr2 = arr[1].split(".");
var prefix = arr2[0];
if (val == prefix){
invalid = true;
return; //this is how to break out of $.each
}
}
});

//Check whether or not input is invalid
if (invalid){
//console.log("ERROR: Matching stuff!")
$('#fb-error-name').text('"'+$('#node-config-input-firebaseurl').val()+'"');
$('#node-config-input-firebaseurl-input-error-container').removeClass('hidden');
$('#node-config-input-firebaseurl').addClass("input-error");
$('#node-config-input-firebaseurl').css("background-color", "#FF6347");
$('#node-config-dialog-ok').prop('disabled', true);
} else {
// console.log("ERROR: Matching stuff!")
if (!$('#node-config-input-firebaseurl-input-error-container').hasClass('hidden'))
$('#node-config-input-firebaseurl-input-error-container').addClass('hidden');

$('#node-config-input-firebaseurl').removeClass("input-error")
$('#node-config-input-firebaseurl').css("background-color", "");
$('#node-config-dialog-ok').prop('disabled', false);
}
});
}


$('.login-row').hide();
if (this.loginType) {
$loginTypeSelect.val(this.loginType); //sets the drop down
Expand Down
24 changes: 12 additions & 12 deletions firebase/firebase_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ module.exports = function (RED) {
//unauthorized
//error //TODO: need to wrap everything in a try/catch to make sure we don't ever crash node-red
//closed
var connectionPool = function(){
var connectionPool = function(){ //TODO: This could probably be refactored to be a bit simpler now...
var connections = {}

return {
get: function(firebaseurl){
if(!connections[firebaseurl]){ //Lazily create a new Firebase Reference if it does not exist
get: function(firebaseurl, configNodeID){
if(!connections[configNodeID]){ //Lazily create a new Firebase Reference if it does not exist

connections[firebaseurl] = function(){
connections[configNodeID] = function(){

//Private
var _emitter = new events.EventEmitter();
Expand All @@ -77,7 +77,7 @@ module.exports = function (RED) {
Firebase: Firebase, //Needed for Firebase.ServerValue.TIMESTAMP...

firebaseurl: firebaseurl, //TODO: Some of this data is duplicated...
fbRef: new Firebase(firebaseurl),
fbRef: new Firebase(firebaseurl, configNodeID), //Including a second argument is a hack which allows us to have multiple auths connected to the same Firebase - see https://github.com/deldrid1/node-red-contrib-firebase/issues/3
authData: null, //TODO: Some of this data is duplicated...
loginType: null,
secret: null,
Expand Down Expand Up @@ -227,20 +227,20 @@ module.exports = function (RED) {
}();
}

connections[firebaseurl].nodeCount++;
connections[configNodeID].nodeCount++;

return connections[firebaseurl]
return connections[configNodeID]
},

close: function(firebaseurl){
var obj = connections[firebaseurl]
close: function(configNodeID){
var obj = connections[configNodeID]

obj.nodeCount--

if(obj.nodeCount == 0){
obj.close()

delete connections[firebaseurl]
delete connections[configNodeID]
//TODO: BUG: there is not way to do close/kill a connection with the current Firebase Library. It is a low priority for them but is scheduled for release middle of 2015... http://stackoverflow.com/questions/27641764/how-to-destroy-firebase-ref-in-node
}
}
Expand All @@ -261,7 +261,7 @@ module.exports = function (RED) {
this.email = this.credentials.email;
this.password = this.credentials.password;

this.fbConnection = connectionPool.get(this.firebaseurl)
this.fbConnection = connectionPool.get(this.firebaseurl, this.id)

this.fbConnection.on("initializing", function(){
// this.log("initializing to " + this.firebaseurl)
Expand Down Expand Up @@ -329,7 +329,7 @@ module.exports = function (RED) {
this.on('close', function() {
this.status({fill: "gray", shape: "dot", text:"connection closed"})
// We need to unbind our callback, or we'll get duplicate messages when we redeploy
connectionPool.close(this.firebaseurl)
connectionPool.close(this.id)
});
}

Expand Down

0 comments on commit 9e3f768

Please sign in to comment.