Skip to content

Commit

Permalink
Added requireEmailVerification argument to $requireSignIn() (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorgan1 authored and Jacob Wenger committed Dec 21, 2016
1 parent 74e8506 commit fffdc0c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
11 changes: 5 additions & 6 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* [`$sendPasswordResetEmail(email)`](#sendpasswordresetemailemail)
* Router Helpers
* [`$waitForSignIn()`](#waitforsignin)
* [`$requireSignIn()`](#requiresignin)
* [`$requireSignIn(requireEmailVerification)`](#requiresignin)
* [Extending the Services](#extending-the-services)
* [Extending `$firebaseObject`](#extending-firebaseobject)
* [Extending `$firebaseArray`](#extending-firebasearray)
Expand Down Expand Up @@ -894,16 +894,15 @@ intended to be used in the `resolve()` method of Angular routers. See the
["Using Authentication with Routers"](/docs/guide/user-auth.md#authenticating-with-routers)
section of our AngularFire guide for more information and a full example.

### $requireSignIn()
### $requireSignIn(requireEmailVerification)

Helper method which returns a promise fulfilled with the current authentication state if the user
is authenticated but otherwise rejects the promise. This is intended to be used in the `resolve()`
method of Angular routers to prevented unauthenticated users from seeing authenticated pages
momentarily during page load. See the
is authenticated and, if specified, has a verified email address, but otherwise rejects the promise.
This is intended to be used in the `resolve()` method of Angular routers to prevented unauthenticated
users from seeing authenticated pages momentarily during page load. See the
["Using Authentication with Routers"](/docs/guide/user-auth.md#authenticating-with-routers)
section of our AngularFire guide for more information and a full example.


## Extending the Services

There are several powerful techniques for transforming the data downloaded and saved
Expand Down
18 changes: 12 additions & 6 deletions src/auth/FirebaseAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
*
* @param {boolean} rejectIfAuthDataIsNull Determines if the returned promise should be
* resolved or rejected upon an unauthenticated client.
* @param {boolean} rejectIfEmailNotVerified Determines if the returned promise should be
* resolved or rejected upon a client without a verified email address.
* @return {Promise<Object>} A promise fulfilled with the client's authentication state or
* rejected if the client is unauthenticated and rejectIfAuthDataIsNull is true.
*/
_routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull) {
_routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull, rejectIfEmailNotVerified) {
var self = this;

// wait for the initial auth state to resolve; on page load we have to request auth state
Expand All @@ -200,6 +202,9 @@
if (rejectIfAuthDataIsNull && authData === null) {
res = self._q.reject("AUTH_REQUIRED");
}
else if (rejectIfEmailNotVerified && !authData.emailVerified) {
res = self._q.reject("EMAIL_VERIFICATION_REQUIRED");
}
else {
res = self._q.when(authData);
}
Expand Down Expand Up @@ -248,11 +253,13 @@
* Utility method which can be used in a route's resolve() method to require that a route has
* a logged in client.
*
* @param {boolean} requireEmailVerification Determines if the route requires a client with a
* verified email address.
* @returns {Promise<Object>} A promise fulfilled with the client's current authentication
* state or rejected if the client is not authenticated.
*/
requireSignIn: function() {
return this._routerMethodOnAuthPromise(true);
requireSignIn: function(requireEmailVerification) {
return this._routerMethodOnAuthPromise(true, requireEmailVerification);
},

/**
Expand All @@ -263,10 +270,9 @@
* state, which will be null if the client is not authenticated.
*/
waitForSignIn: function() {
return this._routerMethodOnAuthPromise(false);
return this._routerMethodOnAuthPromise(false, false);
},



/*********************/
/* User Management */
/*********************/
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/FirebaseAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,57 @@ describe('FirebaseAuth',function(){
tick();
});
});

describe('$requireSignIn(requireEmailVerification)',function(){
it('will be resolved if user is logged in and has a verified email address', function(done){
var credentials = {provider: 'facebook', emailVerified: true};
spyOn(authService._, 'getAuth').and.callFake(function () {
return credentials;
});

authService.$requireSignIn(true)
.then(function (result) {
expect(result).toEqual(credentials);
done();
});

fakePromiseResolve(credentials);
tick();
});

it('will be resolved if user is logged in and we ignore email verification', function(done){
var credentials = {provider: 'facebook', emailVerified: false};
spyOn(authService._, 'getAuth').and.callFake(function () {
return credentials;
});

authService.$requireSignIn(false)
.then(function (result) {
expect(result).toEqual(credentials);
done();
});

fakePromiseResolve(credentials);
tick();
});

it('will be rejected if user does not have a verified email address', function(done){
var credentials = {provider: 'facebook', emailVerified: false};
spyOn(authService._, 'getAuth').and.callFake(function () {
return credentials;
});

authService.$requireSignIn(true)
.catch(function (error) {
expect(error).toEqual('EMAIL_VERIFICATION_REQUIRED');
done();
});

fakePromiseResolve(credentials);
tick();
});
});

describe('$waitForSignIn()',function(){
it('will be resolved with authData if user is logged in', function(done){
var credentials = {provider: 'facebook'};
Expand Down

0 comments on commit fffdc0c

Please sign in to comment.