Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit afc3fe7

Browse files
committed
Refactor FirebaseAuth to use makeNodeResolver
1 parent d6f09ba commit afc3fe7

File tree

1 file changed

+14
-46
lines changed

1 file changed

+14
-46
lines changed

src/FirebaseAuth.js

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Define a service which provides user authentication and management.
77
angular.module('firebase').factory('$firebaseAuth', [
8-
'$q', function($q) {
8+
'$q', '$firebaseUtils', function($q, $firebaseUtils) {
99
// This factory returns an object containing the current authentication state of the client.
1010
// This service takes one argument:
1111
//
@@ -14,14 +14,15 @@
1414
// The returned object contains methods for authenticating clients, retrieving authentication
1515
// state, and managing users.
1616
return function(ref) {
17-
var auth = new FirebaseAuth($q, ref);
17+
var auth = new FirebaseAuth($q, $firebaseUtils, ref);
1818
return auth.construct();
1919
};
2020
}
2121
]);
2222

23-
FirebaseAuth = function($q, ref) {
23+
FirebaseAuth = function($q, $firebaseUtils, ref) {
2424
this._q = $q;
25+
this._utils = $firebaseUtils;
2526

2627
if (typeof ref === 'string') {
2728
throw new Error('Please provide a Firebase reference instead of a URL when creating a `$firebaseAuth` object.');
@@ -61,20 +62,12 @@
6162
/********************/
6263
/* Authentication */
6364
/********************/
64-
// Common login completion handler for all authentication methods.
65-
_onLoginHandler: function(deferred, error, authData) {
66-
if (error !== null) {
67-
deferred.reject(error);
68-
} else {
69-
deferred.resolve(authData);
70-
}
71-
},
7265

7366
// Authenticates the Firebase reference with a custom authentication token.
7467
authWithCustomToken: function(authToken, options) {
7568
var deferred = this._q.defer();
7669

77-
this._ref.authWithCustomToken(authToken, this._onLoginHandler.bind(this, deferred), options);
70+
this._ref.authWithCustomToken(authToken, this._utils.makeNodeResolver(deferred), options);
7871

7972
return deferred.promise;
8073
},
@@ -83,7 +76,7 @@
8376
authAnonymously: function(options) {
8477
var deferred = this._q.defer();
8578

86-
this._ref.authAnonymously(this._onLoginHandler.bind(this, deferred), options);
79+
this._ref.authAnonymously(this._utils.makeNodeResolver(deferred), options);
8780

8881
return deferred.promise;
8982
},
@@ -92,7 +85,7 @@
9285
authWithPassword: function(credentials, options) {
9386
var deferred = this._q.defer();
9487

95-
this._ref.authWithPassword(credentials, this._onLoginHandler.bind(this, deferred), options);
88+
this._ref.authWithPassword(credentials, this._utils.makeNodeResolver(deferred), options);
9689

9790
return deferred.promise;
9891
},
@@ -101,7 +94,7 @@
10194
authWithOAuthPopup: function(provider, options) {
10295
var deferred = this._q.defer();
10396

104-
this._ref.authWithOAuthPopup(provider, this._onLoginHandler.bind(this, deferred), options);
97+
this._ref.authWithOAuthPopup(provider, this._utils.makeNodeResolver(deferred), options);
10598

10699
return deferred.promise;
107100
},
@@ -110,7 +103,7 @@
110103
authWithOAuthRedirect: function(provider, options) {
111104
var deferred = this._q.defer();
112105

113-
this._ref.authWithOAuthRedirect(provider, this._onLoginHandler.bind(this, deferred), options);
106+
this._ref.authWithOAuthRedirect(provider, this._utils.makeNodeResolver(deferred), options);
114107

115108
return deferred.promise;
116109
},
@@ -119,7 +112,7 @@
119112
authWithOAuthToken: function(provider, credentials, options) {
120113
var deferred = this._q.defer();
121114

122-
this._ref.authWithOAuthToken(provider, credentials, this._onLoginHandler.bind(this, deferred), options);
115+
this._ref.authWithOAuthToken(provider, credentials, this._utils.makeNodeResolver(deferred), options);
123116

124117
return deferred.promise;
125118
},
@@ -202,13 +195,7 @@
202195
this._ref.createUser({
203196
email: email,
204197
password: password
205-
}, function(error) {
206-
if (error !== null) {
207-
deferred.reject(error);
208-
} else {
209-
deferred.resolve();
210-
}
211-
});
198+
}, this._utils.makeNodeResolver(deferred));
212199

213200
return deferred.promise;
214201
},
@@ -221,14 +208,7 @@
221208
email: email,
222209
oldPassword: oldPassword,
223210
newPassword: newPassword
224-
}, function(error) {
225-
if (error !== null) {
226-
deferred.reject(error);
227-
} else {
228-
deferred.resolve();
229-
}
230-
}
231-
);
211+
}, this._utils.makeNodeResolver(deferred));
232212

233213
return deferred.promise;
234214
},
@@ -240,13 +220,7 @@
240220
this._ref.removeUser({
241221
email: email,
242222
password: password
243-
}, function(error) {
244-
if (error !== null) {
245-
deferred.reject(error);
246-
} else {
247-
deferred.resolve();
248-
}
249-
});
223+
}, this._utils.makeNodeResolver(deferred));
250224

251225
return deferred.promise;
252226
},
@@ -257,13 +231,7 @@
257231

258232
this._ref.resetPassword({
259233
email: email
260-
}, function(error) {
261-
if (error !== null) {
262-
deferred.reject(error);
263-
} else {
264-
deferred.resolve();
265-
}
266-
});
234+
}, this._utils.makeNodeResolver(deferred));
267235

268236
return deferred.promise;
269237
}

0 commit comments

Comments
 (0)