diff --git a/src/FirebaseAuth.js b/src/FirebaseAuth.js index f8d1f896..37f2036d 100644 --- a/src/FirebaseAuth.js +++ b/src/FirebaseAuth.js @@ -155,37 +155,37 @@ }, // 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(); + + function callback (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); + ref.onAuth(callback); + + 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); },