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

Commit 64dffcb

Browse files
committed
Merge pull request #518 from firebase/jw-change-email
Added $changeEmail() method
2 parents 7422ecf + d0e4ddc commit 64dffcb

File tree

6 files changed

+109
-31
lines changed

6 files changed

+109
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ In order to use AngularFire in your project, you need to include the following f
1818

1919
```html
2020
<!-- AngularJS -->
21-
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script>
21+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
2222

2323
<!-- Firebase -->
24-
<script src="https://cdn.firebase.com/js/client/2.0.6/firebase.js"></script>
24+
<script src="https://cdn.firebase.com/js/client/2.1.0/firebase.js"></script>
2525

2626
<!-- AngularFire -->
2727
<script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script>

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"dependencies": {
3333
"angular": "1.2.x || 1.3.x",
34-
"firebase": "2.0.x"
34+
"firebase": "2.1.x"
3535
},
3636
"devDependencies": {
3737
"lodash": "~2.4.1",

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
feature - Added `$firebaseAuth.$changeEmail()` to change the email address associated with an existing account.
12
feature - `$firebaseAuth.$createUser()` is now fulfilled with a user object which contains the created user's `uid`.
23
feature - Added several minor performance improvements implemented by @jamestalmage.
34
fixed - `$firebaseAuth.$onAuth()` now properly fires a digest loop upon changes in authentication state.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
],
3333
"dependencies": {
3434
"angular": "1.3.x",
35-
"firebase": "2.0.x"
35+
"firebase": "2.1.x"
3636
},
3737
"devDependencies": {
3838
"coveralls": "^2.11.1",

src/FirebaseAuth.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
// User management methods
5252
$createUser: this.createUser.bind(this),
5353
$changePassword: this.changePassword.bind(this),
54+
$changeEmail: this.changeEmail.bind(this),
5455
$removeUser: this.removeUser.bind(this),
5556
$resetPassword: this.resetPassword.bind(this),
5657
$sendPasswordResetEmail: this.sendPasswordResetEmail.bind(this)
@@ -331,7 +332,7 @@
331332
* Changes the password for an email/password user.
332333
*
333334
* @param {Object|string} emailOrCredentials The email of the user whose password is to change
334-
* 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
335336
* is to change.
336337
* @param {string} [oldPassword] The current password for the user.
337338
* @param {string} [newPassword] The new password for the user.
@@ -361,6 +362,29 @@
361362
return deferred.promise;
362363
},
363364

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+
}
384+
385+
return deferred.promise;
386+
},
387+
364388
/**
365389
* Removes an email/password user.
366390
*

tests/unit/FirebaseAuth.spec.js

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('FirebaseAuth',function(){
2626
['authWithCustomToken','authAnonymously','authWithPassword',
2727
'authWithOAuthPopup','authWithOAuthRedirect','authWithOAuthToken',
2828
'unauth','getAuth','onAuth','offAuth',
29-
'createUser','changePassword','removeUser','resetPassword'
29+
'createUser','changePassword','changeEmail','removeUser','resetPassword'
3030
]);
3131

3232
inject(function(_$firebaseAuth_,_$timeout_){
@@ -196,7 +196,7 @@ describe('FirebaseAuth',function(){
196196
expect(result).toEqual('myResult');
197197
});
198198
});
199-
199+
200200
describe('$authWithOAuthToken',function(){
201201
it('passes provider, token, and options object to underlying method',function(){
202202
var provider = 'facebook';
@@ -360,42 +360,95 @@ describe('FirebaseAuth',function(){
360360
expect(result).toEqual({uid:'1234'});
361361
});
362362
});
363-
364-
describe('$changePassword()',function(){
365-
it('passes email/password to method on backing ref (string args)',function(){
366-
auth.$changePassword('[email protected]','54321','12345');
367-
expect(ref.changePassword).toHaveBeenCalledWith(
368-
{email:'[email protected]',oldPassword:'54321',newPassword:'12345'},
369-
jasmine.any(Function));
370-
});
371363

372-
it('passes email/password to method on backing ref (object arg)',function(){
373-
auth.$changePassword({email:'[email protected]',oldPassword:'54321',newPassword:'12345'});
374-
expect(ref.changePassword).toHaveBeenCalledWith(
375-
{email:'[email protected]',oldPassword:'54321',newPassword:'12345'},
376-
jasmine.any(Function));
377-
});
378-
379-
it('will log a warning if deprecated string args are used',function(){
364+
describe('$changePassword()',function() {
365+
it('passes credentials to method on backing ref (string args)',function() {
366+
auth.$changePassword('[email protected]','54321','12345');
367+
expect(ref.changePassword).toHaveBeenCalledWith({
368+
369+
oldPassword: '54321',
370+
newPassword: '12345'
371+
}, jasmine.any(Function));
372+
});
373+
374+
it('passes credentials to method on backing ref (object arg)',function() {
375+
auth.$changePassword({
376+
377+
oldPassword: '54321',
378+
newPassword: '12345'
379+
});
380+
expect(ref.changePassword).toHaveBeenCalledWith({
381+
382+
oldPassword: '54321',
383+
newPassword: '12345'
384+
}, jasmine.any(Function));
385+
});
386+
387+
it('will log a warning if deprecated string args are used',function() {
380388
auth.$changePassword('[email protected]','54321','12345');
381389
expect(log.warn).toHaveLength(1);
382390
});
383391

384-
it('will reject the promise if authentication fails',function(){
385-
wrapPromise(auth.$changePassword({email:'[email protected]',oldPassword:'54321',newPassword:'12345'}));
392+
it('will reject the promise if the password change fails',function() {
393+
wrapPromise(auth.$changePassword({
394+
395+
oldPassword: '54321',
396+
newPassword: '12345'
397+
}));
386398
callback('changePassword')("bad password");
387399
$timeout.flush();
388400
expect(failure).toEqual("bad password");
389401
});
390402

391-
it('will resolve the promise upon authentication',function(){
392-
wrapPromise(auth.$changePassword('[email protected]','54321','12345'));
403+
it('will resolve the promise upon the password change',function() {
404+
wrapPromise(auth.$changePassword({
405+
406+
oldPassword: '54321',
407+
newPassword: '12345'
408+
}));
393409
callback('changePassword')(null);
394410
$timeout.flush();
395411
expect(status).toEqual('resolved');
396412
});
397413
});
398-
414+
415+
describe('$changeEmail()',function() {
416+
it('passes credentials to method on backing reference', function() {
417+
auth.$changeEmail({
418+
oldEmail: '[email protected]',
419+
newEmail: '[email protected]',
420+
password: '12345'
421+
});
422+
expect(ref.changeEmail).toHaveBeenCalledWith({
423+
oldEmail: '[email protected]',
424+
newEmail: '[email protected]',
425+
password: '12345'
426+
}, jasmine.any(Function));
427+
});
428+
429+
it('will reject the promise if the email change fails',function() {
430+
wrapPromise(auth.$changeEmail({
431+
oldEmail: '[email protected]',
432+
newEmail: '[email protected]',
433+
password: '12345'
434+
}));
435+
callback('changeEmail')("bad password");
436+
$timeout.flush();
437+
expect(failure).toEqual("bad password");
438+
});
439+
440+
it('will resolve the promise upon the email change',function() {
441+
wrapPromise(auth.$changeEmail({
442+
oldEmail: '[email protected]',
443+
newEmail: '[email protected]',
444+
password: '12345'
445+
}));
446+
callback('changeEmail')(null);
447+
$timeout.flush();
448+
expect(status).toEqual('resolved');
449+
});
450+
});
451+
399452
describe('$removeUser()',function(){
400453
it('passes email/password to method on backing ref (string args)',function(){
401454
auth.$removeUser('[email protected]','12345');
@@ -430,7 +483,7 @@ describe('FirebaseAuth',function(){
430483
expect(status).toEqual('resolved');
431484
});
432485
});
433-
486+
434487
describe('$sendPasswordResetEmail()',function(){
435488
it('passes email to method on backing ref (string args)',function(){
436489
auth.$sendPasswordResetEmail('[email protected]');
@@ -450,7 +503,7 @@ describe('FirebaseAuth',function(){
450503
auth.$sendPasswordResetEmail({email:'[email protected]'});
451504
expect(log.warn).toHaveLength(1);
452505
});
453-
506+
454507
it('will log two deprecation warnings if string arg is used',function(){
455508
auth.$sendPasswordResetEmail('[email protected]');
456509
expect(log.warn).toHaveLength(2);
@@ -470,7 +523,7 @@ describe('FirebaseAuth',function(){
470523
expect(status).toEqual('resolved');
471524
});
472525
});
473-
526+
474527
describe('$resetPassword()',function(){
475528
it('passes email to method on backing ref (string args)',function(){
476529
auth.$resetPassword('[email protected]');

0 commit comments

Comments
 (0)