From 4298febcd940d0fa1faf8cb93d8e705649f7bcd1 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Wed, 19 Nov 2014 21:42:09 -0500 Subject: [PATCH 1/2] call offAuth() with correct method. closes #471. --- src/FirebaseAuth.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/FirebaseAuth.js b/src/FirebaseAuth.js index f8d1f896..b3abb19c 100644 --- a/src/FirebaseAuth.js +++ b/src/FirebaseAuth.js @@ -155,37 +155,35 @@ }, // Helper onAuth() callback method for the two router-related methods. - _routerMethodOnAuthCallback: function(deferred, rejectIfAuthDataIsNull, authData) { - if (authData !== null) { - deferred.resolve(authData); - } else if (rejectIfAuthDataIsNull) { - deferred.reject("AUTH_REQUIRED"); - } else { - deferred.resolve(null); - } + _routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull) { + var ref = this._ref, + deferred = this._q.defer(); + + var callback = function (authData){ + if (authData !== null) { + deferred.resolve(authData); + } else if (rejectIfAuthDataIsNull) { + deferred.reject("AUTH_REQUIRED"); + } else { + deferred.resolve(null); + } + // Turn off this onAuth() callback since we just needed to get the authentication data once. + ref.offAuth(callback); + }; - // Turn off this onAuth() callback since we just needed to get the authentication data once. - this._ref.offAuth(this._routerMethodOnAuthCallback); + return deferred.promise; }, // Returns a promise which is resolved if the client is authenticated and rejects otherwise. // This can be used to require that a route has a logged in user. requireAuth: function() { - var deferred = this._q.defer(); - - this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ true)); - - return deferred.promise; + return this._routerMethodOnAuthPromise(true); }, // Returns a promise which is resolved with the client's current authenticated data. This can // be used in a route's resolve() method to grab the current authentication data. waitForAuth: function() { - var deferred = this._q.defer(); - - this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ false)); - - return deferred.promise; + return this._routerMethodOnAuthPromise(false); }, From 377155f2a66587834158f72638931361a7727997 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Wed, 19 Nov 2014 21:49:51 -0500 Subject: [PATCH 2/2] Actually pass the callback to ref.onAuth(). --- src/FirebaseAuth.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/FirebaseAuth.js b/src/FirebaseAuth.js index b3abb19c..37f2036d 100644 --- a/src/FirebaseAuth.js +++ b/src/FirebaseAuth.js @@ -159,7 +159,7 @@ var ref = this._ref, deferred = this._q.defer(); - var callback = function (authData){ + function callback (authData){ if (authData !== null) { deferred.resolve(authData); } else if (rejectIfAuthDataIsNull) { @@ -169,7 +169,9 @@ } // Turn off this onAuth() callback since we just needed to get the authentication data once. ref.offAuth(callback); - }; + } + + ref.onAuth(callback); return deferred.promise; },