|
5 | 5 |
|
6 | 6 | // Define a service which provides user authentication and management. |
7 | 7 | angular.module('firebase').factory('$firebaseAuth', [ |
8 | | - '$q', '$log', function($q, $log) { |
| 8 | + '$q', '$firebaseUtils', '$log', function($q, $firebaseUtils, $log) { |
9 | 9 | // This factory returns an object containing the current authentication state of the client. |
10 | 10 | // This service takes one argument: |
11 | 11 | // |
|
14 | 14 | // The returned object contains methods for authenticating clients, retrieving authentication |
15 | 15 | // state, and managing users. |
16 | 16 | return function(ref) { |
17 | | - var auth = new FirebaseAuth($q, $log, ref); |
| 17 | + var auth = new FirebaseAuth($q, $firebaseUtils, $log, ref); |
18 | 18 | return auth.construct(); |
19 | 19 | }; |
20 | 20 | } |
21 | 21 | ]); |
22 | 22 |
|
23 | | - FirebaseAuth = function($q, $log, ref) { |
| 23 | + FirebaseAuth = function($q, $firebaseUtils, $log, ref) { |
24 | 24 | this._q = $q; |
| 25 | + this._utils = $firebaseUtils; |
25 | 26 | this._log = $log; |
26 | 27 |
|
27 | 28 | if (typeof ref === 'string') { |
|
63 | 64 | /********************/ |
64 | 65 | /* Authentication */ |
65 | 66 | /********************/ |
66 | | - /** |
67 | | - * Common login completion handler for all authentication methods. |
68 | | - * |
69 | | - * @param {Promise} deferred A deferred promise which is either resolved or rejected. |
70 | | - * @param {Error|null} error A Firebase error if authentication fails. |
71 | | - * @param {Object|null} authData The authentication state upon successful authentication. |
72 | | - */ |
73 | | - _onLoginHandler: function(deferred, error, authData) { |
74 | | - if (error !== null) { |
75 | | - deferred.reject(error); |
76 | | - } else { |
77 | | - deferred.resolve(authData); |
78 | | - } |
79 | | - }, |
80 | 67 |
|
81 | 68 | /** |
82 | 69 | * Authenticates the Firebase reference with a custom authentication token. |
|
91 | 78 | authWithCustomToken: function(authToken, options) { |
92 | 79 | var deferred = this._q.defer(); |
93 | 80 |
|
94 | | - this._ref.authWithCustomToken(authToken, this._onLoginHandler.bind(this, deferred), options); |
| 81 | + this._ref.authWithCustomToken(authToken, this._utils.makeNodeResolver(deferred), options); |
95 | 82 |
|
96 | 83 | return deferred.promise; |
97 | 84 | }, |
|
106 | 93 | authAnonymously: function(options) { |
107 | 94 | var deferred = this._q.defer(); |
108 | 95 |
|
109 | | - this._ref.authAnonymously(this._onLoginHandler.bind(this, deferred), options); |
| 96 | + this._ref.authAnonymously(this._utils.makeNodeResolver(deferred), options); |
110 | 97 |
|
111 | 98 | return deferred.promise; |
112 | 99 | }, |
|
123 | 110 | authWithPassword: function(credentials, options) { |
124 | 111 | var deferred = this._q.defer(); |
125 | 112 |
|
126 | | - this._ref.authWithPassword(credentials, this._onLoginHandler.bind(this, deferred), options); |
| 113 | + this._ref.authWithPassword(credentials, this._utils.makeNodeResolver(deferred), options); |
127 | 114 |
|
128 | 115 | return deferred.promise; |
129 | 116 | }, |
|
140 | 127 | authWithOAuthPopup: function(provider, options) { |
141 | 128 | var deferred = this._q.defer(); |
142 | 129 |
|
143 | | - this._ref.authWithOAuthPopup(provider, this._onLoginHandler.bind(this, deferred), options); |
| 130 | + this._ref.authWithOAuthPopup(provider, this._utils.makeNodeResolver(deferred), options); |
144 | 131 |
|
145 | 132 | return deferred.promise; |
146 | 133 | }, |
|
157 | 144 | authWithOAuthRedirect: function(provider, options) { |
158 | 145 | var deferred = this._q.defer(); |
159 | 146 |
|
160 | | - this._ref.authWithOAuthRedirect(provider, this._onLoginHandler.bind(this, deferred), options); |
| 147 | + this._ref.authWithOAuthRedirect(provider, this._utils.makeNodeResolver(deferred), options); |
161 | 148 |
|
162 | 149 | return deferred.promise; |
163 | 150 | }, |
|
176 | 163 | authWithOAuthToken: function(provider, credentials, options) { |
177 | 164 | var deferred = this._q.defer(); |
178 | 165 |
|
179 | | - this._ref.authWithOAuthToken(provider, credentials, this._onLoginHandler.bind(this, deferred), options); |
| 166 | + this._ref.authWithOAuthToken(provider, credentials, this._utils.makeNodeResolver(deferred), options); |
180 | 167 |
|
181 | 168 | return deferred.promise; |
182 | 169 | }, |
|
307 | 294 | }; |
308 | 295 | } |
309 | 296 |
|
310 | | - this._ref.createUser(credentials, function(error) { |
311 | | - if (error !== null) { |
312 | | - deferred.reject(error); |
313 | | - } else { |
314 | | - deferred.resolve(user); |
315 | | - } |
316 | | - }); |
| 297 | + this._ref.createUser(credentials, this._utils.makeNodeResolver(deferred)); |
317 | 298 |
|
318 | 299 | return deferred.promise; |
319 | 300 | }, |
|
343 | 324 | }; |
344 | 325 | } |
345 | 326 |
|
346 | | - this._ref.changePassword(credentials, function(error) { |
347 | | - if (error !== null) { |
348 | | - deferred.reject(error); |
349 | | - } else { |
350 | | - deferred.resolve(); |
351 | | - } |
352 | | - }); |
| 327 | + this._ref.changePassword(credentials, this._utils.makeNodeResolver(deferred)); |
353 | 328 |
|
354 | 329 | return deferred.promise; |
355 | 330 | }, |
|
376 | 351 | }; |
377 | 352 | } |
378 | 353 |
|
379 | | - this._ref.removeUser(credentials, function(error) { |
380 | | - if (error !== null) { |
381 | | - deferred.reject(error); |
382 | | - } else { |
383 | | - deferred.resolve(); |
384 | | - } |
385 | | - }); |
| 354 | + this._ref.removeUser(credentials, this._utils.makeNodeResolver(deferred)); |
386 | 355 |
|
387 | 356 | return deferred.promise; |
388 | 357 | }, |
|
420 | 389 | }; |
421 | 390 | } |
422 | 391 |
|
423 | | - this._ref.resetPassword(credentials, function(error) { |
424 | | - if (error !== null) { |
425 | | - deferred.reject(error); |
426 | | - } else { |
427 | | - deferred.resolve(); |
428 | | - } |
429 | | - }); |
| 392 | + this._ref.resetPassword(credentials, this._utils.makeNodeResolver(deferred)); |
430 | 393 |
|
431 | 394 | return deferred.promise; |
432 | 395 | } |
|
0 commit comments