|
155 | 155 | }, |
156 | 156 |
|
157 | 157 | // Helper onAuth() callback method for the two router-related methods. |
158 | | - _routerMethodOnAuthCallback: function(deferred, rejectIfAuthDataIsNull, authData) { |
159 | | - if (authData !== null) { |
160 | | - deferred.resolve(authData); |
161 | | - } else if (rejectIfAuthDataIsNull) { |
162 | | - deferred.reject("AUTH_REQUIRED"); |
163 | | - } else { |
164 | | - deferred.resolve(null); |
165 | | - } |
| 158 | + _routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull) { |
| 159 | + var ref = this._ref, |
| 160 | + deferred = this._q.defer(); |
| 161 | + |
| 162 | + var callback = function (authData){ |
| 163 | + if (authData !== null) { |
| 164 | + deferred.resolve(authData); |
| 165 | + } else if (rejectIfAuthDataIsNull) { |
| 166 | + deferred.reject("AUTH_REQUIRED"); |
| 167 | + } else { |
| 168 | + deferred.resolve(null); |
| 169 | + } |
| 170 | + // Turn off this onAuth() callback since we just needed to get the authentication data once. |
| 171 | + ref.offAuth(callback); |
| 172 | + }; |
166 | 173 |
|
167 | | - // Turn off this onAuth() callback since we just needed to get the authentication data once. |
168 | | - this._ref.offAuth(this._routerMethodOnAuthCallback); |
| 174 | + return deferred.promise; |
169 | 175 | }, |
170 | 176 |
|
171 | 177 | // Returns a promise which is resolved if the client is authenticated and rejects otherwise. |
172 | 178 | // This can be used to require that a route has a logged in user. |
173 | 179 | requireAuth: function() { |
174 | | - var deferred = this._q.defer(); |
175 | | - |
176 | | - this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ true)); |
177 | | - |
178 | | - return deferred.promise; |
| 180 | + return this._routerMethodOnAuthPromise(true); |
179 | 181 | }, |
180 | 182 |
|
181 | 183 | // Returns a promise which is resolved with the client's current authenticated data. This can |
182 | 184 | // be used in a route's resolve() method to grab the current authentication data. |
183 | 185 | waitForAuth: function() { |
184 | | - var deferred = this._q.defer(); |
185 | | - |
186 | | - this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ false)); |
187 | | - |
188 | | - return deferred.promise; |
| 186 | + return this._routerMethodOnAuthPromise(false); |
189 | 187 | }, |
190 | 188 |
|
191 | 189 |
|
|
0 commit comments