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

Commit fffdc0c

Browse files
ericmorgan1Jacob Wenger
authored andcommitted
Added requireEmailVerification argument to $requireSignIn() (#887)
1 parent 74e8506 commit fffdc0c

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

docs/reference.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* [`$sendPasswordResetEmail(email)`](#sendpasswordresetemailemail)
4444
* Router Helpers
4545
* [`$waitForSignIn()`](#waitforsignin)
46-
* [`$requireSignIn()`](#requiresignin)
46+
* [`$requireSignIn(requireEmailVerification)`](#requiresignin)
4747
* [Extending the Services](#extending-the-services)
4848
* [Extending `$firebaseObject`](#extending-firebaseobject)
4949
* [Extending `$firebaseArray`](#extending-firebasearray)
@@ -894,16 +894,15 @@ intended to be used in the `resolve()` method of Angular routers. See the
894894
["Using Authentication with Routers"](/docs/guide/user-auth.md#authenticating-with-routers)
895895
section of our AngularFire guide for more information and a full example.
896896

897-
### $requireSignIn()
897+
### $requireSignIn(requireEmailVerification)
898898

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

906-
907906
## Extending the Services
908907

909908
There are several powerful techniques for transforming the data downloaded and saved

src/auth/FirebaseAuth.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,12 @@
184184
*
185185
* @param {boolean} rejectIfAuthDataIsNull Determines if the returned promise should be
186186
* resolved or rejected upon an unauthenticated client.
187+
* @param {boolean} rejectIfEmailNotVerified Determines if the returned promise should be
188+
* resolved or rejected upon a client without a verified email address.
187189
* @return {Promise<Object>} A promise fulfilled with the client's authentication state or
188190
* rejected if the client is unauthenticated and rejectIfAuthDataIsNull is true.
189191
*/
190-
_routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull) {
192+
_routerMethodOnAuthPromise: function(rejectIfAuthDataIsNull, rejectIfEmailNotVerified) {
191193
var self = this;
192194

193195
// wait for the initial auth state to resolve; on page load we have to request auth state
@@ -200,6 +202,9 @@
200202
if (rejectIfAuthDataIsNull && authData === null) {
201203
res = self._q.reject("AUTH_REQUIRED");
202204
}
205+
else if (rejectIfEmailNotVerified && !authData.emailVerified) {
206+
res = self._q.reject("EMAIL_VERIFICATION_REQUIRED");
207+
}
203208
else {
204209
res = self._q.when(authData);
205210
}
@@ -248,11 +253,13 @@
248253
* Utility method which can be used in a route's resolve() method to require that a route has
249254
* a logged in client.
250255
*
256+
* @param {boolean} requireEmailVerification Determines if the route requires a client with a
257+
* verified email address.
251258
* @returns {Promise<Object>} A promise fulfilled with the client's current authentication
252259
* state or rejected if the client is not authenticated.
253260
*/
254-
requireSignIn: function() {
255-
return this._routerMethodOnAuthPromise(true);
261+
requireSignIn: function(requireEmailVerification) {
262+
return this._routerMethodOnAuthPromise(true, requireEmailVerification);
256263
},
257264

258265
/**
@@ -263,10 +270,9 @@
263270
* state, which will be null if the client is not authenticated.
264271
*/
265272
waitForSignIn: function() {
266-
return this._routerMethodOnAuthPromise(false);
273+
return this._routerMethodOnAuthPromise(false, false);
267274
},
268-
269-
275+
270276
/*********************/
271277
/* User Management */
272278
/*********************/

tests/unit/FirebaseAuth.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,57 @@ describe('FirebaseAuth',function(){
384384
tick();
385385
});
386386
});
387+
388+
describe('$requireSignIn(requireEmailVerification)',function(){
389+
it('will be resolved if user is logged in and has a verified email address', function(done){
390+
var credentials = {provider: 'facebook', emailVerified: true};
391+
spyOn(authService._, 'getAuth').and.callFake(function () {
392+
return credentials;
393+
});
394+
395+
authService.$requireSignIn(true)
396+
.then(function (result) {
397+
expect(result).toEqual(credentials);
398+
done();
399+
});
387400

401+
fakePromiseResolve(credentials);
402+
tick();
403+
});
404+
405+
it('will be resolved if user is logged in and we ignore email verification', function(done){
406+
var credentials = {provider: 'facebook', emailVerified: false};
407+
spyOn(authService._, 'getAuth').and.callFake(function () {
408+
return credentials;
409+
});
410+
411+
authService.$requireSignIn(false)
412+
.then(function (result) {
413+
expect(result).toEqual(credentials);
414+
done();
415+
});
416+
417+
fakePromiseResolve(credentials);
418+
tick();
419+
});
420+
421+
it('will be rejected if user does not have a verified email address', function(done){
422+
var credentials = {provider: 'facebook', emailVerified: false};
423+
spyOn(authService._, 'getAuth').and.callFake(function () {
424+
return credentials;
425+
});
426+
427+
authService.$requireSignIn(true)
428+
.catch(function (error) {
429+
expect(error).toEqual('EMAIL_VERIFICATION_REQUIRED');
430+
done();
431+
});
432+
433+
fakePromiseResolve(credentials);
434+
tick();
435+
});
436+
});
437+
388438
describe('$waitForSignIn()',function(){
389439
it('will be resolved with authData if user is logged in', function(done){
390440
var credentials = {provider: 'facebook'};

0 commit comments

Comments
 (0)