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

Commit b512baa

Browse files
committed
Merge pull request #488 from jamestalmage/utils-node-resolver
utils: add makeNodeResolver function.
2 parents 38c509f + 5d58be7 commit b512baa

File tree

3 files changed

+64
-51
lines changed

3 files changed

+64
-51
lines changed

src/FirebaseAuth.js

Lines changed: 14 additions & 51 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', '$log', function($q, $log) {
8+
'$q', '$firebaseUtils', '$log', function($q, $firebaseUtils, $log) {
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, $log, ref);
17+
var auth = new FirebaseAuth($q, $firebaseUtils, $log, ref);
1818
return auth.construct();
1919
};
2020
}
2121
]);
2222

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

2728
if (typeof ref === 'string') {
@@ -63,20 +64,6 @@
6364
/********************/
6465
/* Authentication */
6566
/********************/
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-
},
8067

8168
/**
8269
* Authenticates the Firebase reference with a custom authentication token.
@@ -91,7 +78,7 @@
9178
authWithCustomToken: function(authToken, options) {
9279
var deferred = this._q.defer();
9380

94-
this._ref.authWithCustomToken(authToken, this._onLoginHandler.bind(this, deferred), options);
81+
this._ref.authWithCustomToken(authToken, this._utils.makeNodeResolver(deferred), options);
9582

9683
return deferred.promise;
9784
},
@@ -106,7 +93,7 @@
10693
authAnonymously: function(options) {
10794
var deferred = this._q.defer();
10895

109-
this._ref.authAnonymously(this._onLoginHandler.bind(this, deferred), options);
96+
this._ref.authAnonymously(this._utils.makeNodeResolver(deferred), options);
11097

11198
return deferred.promise;
11299
},
@@ -123,7 +110,7 @@
123110
authWithPassword: function(credentials, options) {
124111
var deferred = this._q.defer();
125112

126-
this._ref.authWithPassword(credentials, this._onLoginHandler.bind(this, deferred), options);
113+
this._ref.authWithPassword(credentials, this._utils.makeNodeResolver(deferred), options);
127114

128115
return deferred.promise;
129116
},
@@ -140,7 +127,7 @@
140127
authWithOAuthPopup: function(provider, options) {
141128
var deferred = this._q.defer();
142129

143-
this._ref.authWithOAuthPopup(provider, this._onLoginHandler.bind(this, deferred), options);
130+
this._ref.authWithOAuthPopup(provider, this._utils.makeNodeResolver(deferred), options);
144131

145132
return deferred.promise;
146133
},
@@ -157,7 +144,7 @@
157144
authWithOAuthRedirect: function(provider, options) {
158145
var deferred = this._q.defer();
159146

160-
this._ref.authWithOAuthRedirect(provider, this._onLoginHandler.bind(this, deferred), options);
147+
this._ref.authWithOAuthRedirect(provider, this._utils.makeNodeResolver(deferred), options);
161148

162149
return deferred.promise;
163150
},
@@ -176,7 +163,7 @@
176163
authWithOAuthToken: function(provider, credentials, options) {
177164
var deferred = this._q.defer();
178165

179-
this._ref.authWithOAuthToken(provider, credentials, this._onLoginHandler.bind(this, deferred), options);
166+
this._ref.authWithOAuthToken(provider, credentials, this._utils.makeNodeResolver(deferred), options);
180167

181168
return deferred.promise;
182169
},
@@ -307,13 +294,7 @@
307294
};
308295
}
309296

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));
317298

318299
return deferred.promise;
319300
},
@@ -343,13 +324,7 @@
343324
};
344325
}
345326

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));
353328

354329
return deferred.promise;
355330
},
@@ -376,13 +351,7 @@
376351
};
377352
}
378353

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));
386355

387356
return deferred.promise;
388357
},
@@ -420,13 +389,7 @@
420389
};
421390
}
422391

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));
430393

431394
return deferred.promise;
432395
}

src/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@
236236
return def.promise;
237237
},
238238

239+
makeNodeResolver:function(deferred){
240+
return function(err,result){
241+
if(err === null){
242+
if(arguments.length > 2){
243+
result = Array.prototype.slice.call(arguments,1);
244+
}
245+
deferred.resolve(result);
246+
}
247+
else {
248+
deferred.reject(err);
249+
}
250+
};
251+
},
252+
239253
wait: function(fn, wait) {
240254
var to = $timeout(fn, wait||0);
241255
return function() {

tests/unit/utils.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,40 @@ describe('$firebaseUtils', function () {
217217
});
218218
});
219219

220+
describe('#makeNodeResolver', function(){
221+
var deferred, callback;
222+
beforeEach(function(){
223+
deferred = jasmine.createSpyObj('promise',['resolve','reject']);
224+
callback = $utils.makeNodeResolver(deferred);
225+
});
226+
227+
it('should return a function', function(){
228+
expect(callback).toBeA('function');
229+
});
230+
231+
it('should reject the promise if the first argument is truthy', function(){
232+
var error = new Error('blah');
233+
callback(error);
234+
expect(deferred.reject).toHaveBeenCalledWith(error);
235+
});
236+
237+
it('should reject the promise if the first argument is not null', function(){
238+
callback(false);
239+
expect(deferred.reject).toHaveBeenCalledWith(false);
240+
});
241+
242+
it('should aggregate multiple args into an array', function(){
243+
var result1 = {data:'hello world!'};
244+
var result2 = {data:'howdy!'};
245+
callback(null,result1,result2);
246+
expect(deferred.resolve).toHaveBeenCalledWith([result1,result2]);
247+
});
248+
249+
it('should resolve the promise if the first argument is falsy', function(){
250+
var result = {data:'hello world'};
251+
callback(null,result);
252+
expect(deferred.resolve).toHaveBeenCalledWith(result);
253+
});
254+
});
255+
220256
});

0 commit comments

Comments
 (0)