|
5 | 5 | // Define a service which provides user authentication and management. |
6 | 6 | angular.module('firebase').factory('$firebaseAuth', [ |
7 | 7 | '$q', '$firebaseUtils', '$log', function($q, $firebaseUtils, $log) { |
8 | | - // This factory returns an object containing the current authentication state of the client. |
9 | | - // This service takes one argument: |
10 | | - // |
11 | | - // * `ref`: A Firebase reference. |
12 | | - // |
13 | | - // The returned object contains methods for authenticating clients, retrieving authentication |
14 | | - // state, and managing users. |
| 8 | + /** |
| 9 | + * This factory returns an object allowing you to manage the client's authentication state. |
| 10 | + * |
| 11 | + * @param {Firebase} ref A Firebase reference to authenticate. |
| 12 | + * @return {object} An object containing methods for authenticating clients, retrieving |
| 13 | + * authentication state, and managing users. |
| 14 | + */ |
15 | 15 | return function(ref) { |
16 | 16 | var auth = new FirebaseAuth($q, $firebaseUtils, $log, ref); |
17 | 17 | return auth.construct(); |
|
51 | 51 | // User management methods |
52 | 52 | $createUser: this.createUser.bind(this), |
53 | 53 | $changePassword: this.changePassword.bind(this), |
| 54 | + $changeEmail: this.changeEmail.bind(this), |
54 | 55 | $removeUser: this.removeUser.bind(this), |
55 | 56 | $resetPassword: this.resetPassword.bind(this), |
56 | 57 | $sendPasswordResetEmail: this.sendPasswordResetEmail.bind(this) |
|
77 | 78 | authWithCustomToken: function(authToken, options) { |
78 | 79 | var deferred = this._q.defer(); |
79 | 80 |
|
80 | | - this._ref.authWithCustomToken(authToken, this._utils.makeNodeResolver(deferred), options); |
| 81 | + try { |
| 82 | + this._ref.authWithCustomToken(authToken, this._utils.makeNodeResolver(deferred), options); |
| 83 | + } catch (error) { |
| 84 | + deferred.reject(error); |
| 85 | + } |
81 | 86 |
|
82 | 87 | return deferred.promise; |
83 | 88 | }, |
|
92 | 97 | authAnonymously: function(options) { |
93 | 98 | var deferred = this._q.defer(); |
94 | 99 |
|
95 | | - this._ref.authAnonymously(this._utils.makeNodeResolver(deferred), options); |
| 100 | + try { |
| 101 | + this._ref.authAnonymously(this._utils.makeNodeResolver(deferred), options); |
| 102 | + } catch (error) { |
| 103 | + deferred.reject(error); |
| 104 | + } |
96 | 105 |
|
97 | 106 | return deferred.promise; |
98 | 107 | }, |
|
109 | 118 | authWithPassword: function(credentials, options) { |
110 | 119 | var deferred = this._q.defer(); |
111 | 120 |
|
112 | | - this._ref.authWithPassword(credentials, this._utils.makeNodeResolver(deferred), options); |
| 121 | + try { |
| 122 | + this._ref.authWithPassword(credentials, this._utils.makeNodeResolver(deferred), options); |
| 123 | + } catch (error) { |
| 124 | + deferred.reject(error); |
| 125 | + } |
113 | 126 |
|
114 | 127 | return deferred.promise; |
115 | 128 | }, |
|
126 | 139 | authWithOAuthPopup: function(provider, options) { |
127 | 140 | var deferred = this._q.defer(); |
128 | 141 |
|
129 | | - this._ref.authWithOAuthPopup(provider, this._utils.makeNodeResolver(deferred), options); |
| 142 | + try { |
| 143 | + this._ref.authWithOAuthPopup(provider, this._utils.makeNodeResolver(deferred), options); |
| 144 | + } catch (error) { |
| 145 | + deferred.reject(error); |
| 146 | + } |
130 | 147 |
|
131 | 148 | return deferred.promise; |
132 | 149 | }, |
|
143 | 160 | authWithOAuthRedirect: function(provider, options) { |
144 | 161 | var deferred = this._q.defer(); |
145 | 162 |
|
146 | | - this._ref.authWithOAuthRedirect(provider, this._utils.makeNodeResolver(deferred), options); |
| 163 | + try { |
| 164 | + this._ref.authWithOAuthRedirect(provider, this._utils.makeNodeResolver(deferred), options); |
| 165 | + } catch (error) { |
| 166 | + deferred.reject(error); |
| 167 | + } |
147 | 168 |
|
148 | 169 | return deferred.promise; |
149 | 170 | }, |
|
162 | 183 | authWithOAuthToken: function(provider, credentials, options) { |
163 | 184 | var deferred = this._q.defer(); |
164 | 185 |
|
165 | | - this._ref.authWithOAuthToken(provider, credentials, this._utils.makeNodeResolver(deferred), options); |
| 186 | + try { |
| 187 | + this._ref.authWithOAuthToken(provider, credentials, this._utils.makeNodeResolver(deferred), options); |
| 188 | + } catch (error) { |
| 189 | + deferred.reject(error); |
| 190 | + } |
166 | 191 |
|
167 | 192 | return deferred.promise; |
168 | 193 | }, |
|
195 | 220 | onAuth: function(callback, context) { |
196 | 221 | var self = this; |
197 | 222 |
|
198 | | - this._ref.onAuth(callback, context); |
| 223 | + var fn = this._utils.debounce(callback, context, 0); |
| 224 | + this._ref.onAuth(fn); |
199 | 225 |
|
200 | 226 | // Return a method to detach the `onAuth()` callback. |
201 | 227 | return function() { |
202 | | - self._ref.offAuth(callback, context); |
| 228 | + self._ref.offAuth(fn); |
203 | 229 | }; |
204 | 230 | }, |
205 | 231 |
|
|
293 | 319 | }; |
294 | 320 | } |
295 | 321 |
|
296 | | - this._ref.createUser(credentials, this._utils.makeNodeResolver(deferred)); |
| 322 | + try { |
| 323 | + this._ref.createUser(credentials, this._utils.makeNodeResolver(deferred)); |
| 324 | + } catch (error) { |
| 325 | + deferred.reject(error); |
| 326 | + } |
297 | 327 |
|
298 | 328 | return deferred.promise; |
299 | 329 | }, |
|
302 | 332 | * Changes the password for an email/password user. |
303 | 333 | * |
304 | 334 | * @param {Object|string} emailOrCredentials The email of the user whose password is to change |
305 | | - * or an objet containing the email, old password, and new password of the user whose password |
| 335 | + * or an object containing the email, old password, and new password of the user whose password |
306 | 336 | * is to change. |
307 | 337 | * @param {string} [oldPassword] The current password for the user. |
308 | 338 | * @param {string} [newPassword] The new password for the user. |
|
323 | 353 | }; |
324 | 354 | } |
325 | 355 |
|
326 | | - this._ref.changePassword(credentials, this._utils.makeNodeResolver(deferred)); |
| 356 | + try { |
| 357 | + this._ref.changePassword(credentials, this._utils.makeNodeResolver(deferred)); |
| 358 | + } catch (error) { |
| 359 | + deferred.reject(error); |
| 360 | + } |
| 361 | + |
| 362 | + return deferred.promise; |
| 363 | + }, |
| 364 | + |
| 365 | + /** |
| 366 | + * Changes the email for an email/password user. |
| 367 | + * |
| 368 | + * @param {Object} credentials An object containing the old email, new email, and password of |
| 369 | + * the user whose email is to change. |
| 370 | + * @return {Promise<>} An empty promise fulfilled once the email change is complete. |
| 371 | + */ |
| 372 | + changeEmail: function(credentials) { |
| 373 | + if (typeof this._ref.changeEmail !== 'function') { |
| 374 | + throw new Error('$firebaseAuth.$changeEmail() requires Firebase version 2.1.0 or greater.'); |
| 375 | + } |
| 376 | + |
| 377 | + var deferred = this._q.defer(); |
| 378 | + |
| 379 | + try { |
| 380 | + this._ref.changeEmail(credentials, this._utils.makeNodeResolver(deferred)); |
| 381 | + } catch (error) { |
| 382 | + deferred.reject(error); |
| 383 | + } |
327 | 384 |
|
328 | 385 | return deferred.promise; |
329 | 386 | }, |
|
350 | 407 | }; |
351 | 408 | } |
352 | 409 |
|
353 | | - this._ref.removeUser(credentials, this._utils.makeNodeResolver(deferred)); |
| 410 | + try { |
| 411 | + this._ref.removeUser(credentials, this._utils.makeNodeResolver(deferred)); |
| 412 | + } catch (error) { |
| 413 | + deferred.reject(error); |
| 414 | + } |
354 | 415 |
|
355 | 416 | return deferred.promise; |
356 | 417 | }, |
|
365 | 426 | */ |
366 | 427 | sendPasswordResetEmail: function(emailOrCredentials) { |
367 | 428 | this._log.warn("$sendPasswordResetEmail() has been deprecated in favor of the equivalent $resetPassword()."); |
368 | | - return this.resetPassword(emailOrCredentials); |
| 429 | + |
| 430 | + try { |
| 431 | + return this.resetPassword(emailOrCredentials); |
| 432 | + } catch (error) { |
| 433 | + return this._q(function(resolve, reject) { |
| 434 | + return reject(error); |
| 435 | + }); |
| 436 | + } |
369 | 437 | }, |
370 | 438 |
|
371 | 439 | /** |
|
388 | 456 | }; |
389 | 457 | } |
390 | 458 |
|
391 | | - this._ref.resetPassword(credentials, this._utils.makeNodeResolver(deferred)); |
| 459 | + try { |
| 460 | + this._ref.resetPassword(credentials, this._utils.makeNodeResolver(deferred)); |
| 461 | + } catch (error) { |
| 462 | + deferred.reject(error); |
| 463 | + } |
392 | 464 |
|
393 | 465 | return deferred.promise; |
394 | 466 | } |
|
0 commit comments