|
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); |
| 158 | + _routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull) { |
| 159 | + var ref = this._ref; |
| 160 | + var deferred = this._q.defer(); |
| 161 | + |
| 162 | + function callback(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 | + |
| 171 | + // Turn off this onAuth() callback since we just needed to get the authentication data once. |
| 172 | + ref.offAuth(callback); |
165 | 173 | } |
166 | 174 |
|
167 | | - // Turn off this onAuth() callback since we just needed to get the authentication data once. |
168 | | - this._ref.offAuth(this._routerMethodOnAuthCallback); |
| 175 | + ref.onAuth(callback); |
| 176 | + |
| 177 | + return deferred.promise; |
169 | 178 | }, |
170 | 179 |
|
171 | 180 | // Returns a promise which is resolved if the client is authenticated and rejects otherwise. |
172 | 181 | // This can be used to require that a route has a logged in user. |
173 | 182 | 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; |
| 183 | + return this._routerMethodOnAuthPromise(true); |
179 | 184 | }, |
180 | 185 |
|
181 | 186 | // Returns a promise which is resolved with the client's current authenticated data. This can |
182 | 187 | // be used in a route's resolve() method to grab the current authentication data. |
183 | 188 | 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; |
| 189 | + return this._routerMethodOnAuthPromise(false); |
189 | 190 | }, |
190 | 191 |
|
191 | 192 |
|
|
0 commit comments