Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 6a6b630

Browse files
itelolirantal
authored andcommitted
feat(users): change username to usernameOrEmail in signin (#1545)
* feat(users): change username to usernameOrEmail in signin * fix(users): toLowerCase at email in local strategy
1 parent 73a7c2c commit 6a6b630

File tree

7 files changed

+64
-29
lines changed

7 files changed

+64
-29
lines changed

modules/articles/tests/server/admin.article.server.routes.tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Article Admin CRUD tests', function () {
3232
beforeEach(function (done) {
3333
// Create user credentials
3434
credentials = {
35-
username: 'username',
35+
usernameOrEmail: 'username',
3636
password: '[email protected]$Aw3$0m3'
3737
};
3838

@@ -43,7 +43,7 @@ describe('Article Admin CRUD tests', function () {
4343
displayName: 'Full Name',
4444
4545
roles: ['user', 'admin'],
46-
username: credentials.username,
46+
username: credentials.usernameOrEmail,
4747
password: credentials.password,
4848
provider: 'local'
4949
});

modules/articles/tests/server/article.server.routes.tests.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Article CRUD tests', function () {
3333
beforeEach(function (done) {
3434
// Create user credentials
3535
credentials = {
36-
username: 'username',
36+
usernameOrEmail: 'username',
3737
password: '[email protected]$Aw3$0m3'
3838
};
3939

@@ -43,7 +43,7 @@ describe('Article CRUD tests', function () {
4343
lastName: 'Name',
4444
displayName: 'Full Name',
4545
46-
username: credentials.username,
46+
username: credentials.usernameOrEmail,
4747
password: credentials.password,
4848
provider: 'local'
4949
});
@@ -216,7 +216,7 @@ describe('Article CRUD tests', function () {
216216
it('should be able to get a single article that has an orphaned user reference', function (done) {
217217
// Create orphan user creds
218218
var _creds = {
219-
username: 'orphan',
219+
usernameOrEmail: 'orphan',
220220
password: '[email protected]$Aw3$0m3'
221221
};
222222

@@ -226,7 +226,7 @@ describe('Article CRUD tests', function () {
226226
lastName: 'Name',
227227
displayName: 'Full Name',
228228
229-
username: _creds.username,
229+
username: _creds.usernameOrEmail,
230230
password: _creds.password,
231231
provider: 'local',
232232
roles: ['admin']
@@ -322,7 +322,7 @@ describe('Article CRUD tests', function () {
322322
it('should be able to get single article, that a different user created, if logged in & verify the "isCurrentUserOwner" field is set to "false"', function (done) {
323323
// Create temporary user creds
324324
var _creds = {
325-
username: 'articleowner',
325+
usernameOrEmail: 'articleowner',
326326
password: '[email protected]$Aw3$0m3'
327327
};
328328

@@ -332,7 +332,7 @@ describe('Article CRUD tests', function () {
332332
lastName: 'Name',
333333
displayName: 'Full Name',
334334
335-
username: _creds.username,
335+
username: _creds.usernameOrEmail,
336336
password: _creds.password,
337337
provider: 'local',
338338
roles: ['admin', 'user']

modules/users/client/views/authentication/signin.client.view.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ <h3 class="col-xs-12 text-center">Or with your account</h3>
77
<uib-alert type="danger" ng-show="vm.error" class="text-center text-danger">
88
<span ng-bind="vm.error"></span>
99
</uib-alert>
10-
<label for="username">Username</label>
11-
<input type="text" id="username" name="username" class="form-control" ng-model="vm.credentials.username" placeholder="Username" lowercase required autofocus>
12-
<div ng-messages="vm.userForm.username.$error" role="alert">
13-
<p class="help-block error-text" ng-message="required">Username is required.</p>
10+
<label for="usernameOrEmail">Username or Email</label>
11+
<input type="text" id="usernameOrEmail" name="usernameOrEmail" class="form-control" ng-model="vm.credentials.usernameOrEmail" placeholder="Username or Email" required autofocus>
12+
<div ng-messages="vm.userForm.usernameOrEmail.$error" role="alert">
13+
<p class="help-block error-text" ng-message="required">Username or Email is required.</p>
1414
</div>
1515
</div>
1616
<div class="form-group" show-errors>

modules/users/server/config/strategies/local.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ var passport = require('passport'),
1010
module.exports = function () {
1111
// Use local strategy
1212
passport.use(new LocalStrategy({
13-
usernameField: 'username',
13+
usernameField: 'usernameOrEmail',
1414
passwordField: 'password'
1515
},
16-
function (username, password, done) {
16+
function (usernameOrEmail, password, done) {
1717
User.findOne({
18-
username: username.toLowerCase()
18+
$or: [{
19+
username: usernameOrEmail.toLowerCase()
20+
}, {
21+
email: usernameOrEmail.toLowerCase()
22+
}]
1923
}, function (err, user) {
2024
if (err) {
2125
return done(err);

modules/users/tests/client/authentication.client.controller.tests.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
}));
4949

5050
describe('$scope.signin()', function () {
51-
it('should login with a correct user and password', function () {
51+
it('should login with a correct username and password', function () {
5252
// Test expected GET request
5353
$httpBackend.when('POST', 'api/auth/signin').respond(200, { username: 'Fred' });
5454

@@ -60,6 +60,18 @@
6060
expect($location.url()).toEqual('/');
6161
});
6262

63+
it('should login with a correct email and password', function () {
64+
// Test expected GET request
65+
$httpBackend.when('POST', 'api/auth/signin').respond(200, { email: '[email protected]' });
66+
67+
scope.vm.signin(true);
68+
$httpBackend.flush();
69+
70+
// Test scope value
71+
expect(scope.vm.authentication.user.email).toEqual('[email protected]');
72+
expect($location.url()).toEqual('/');
73+
});
74+
6375
it('should be redirected to previous state after successful login',
6476
inject(function (_$state_) {
6577
$state = _$state_;
@@ -101,7 +113,7 @@
101113

102114
it('should fail to log in with wrong credentials', function () {
103115
// Foo/Bar combo assumed to not exist
104-
scope.vm.authentication.user = { usersname: 'Foo' };
116+
scope.vm.authentication.user = { username: 'Foo' };
105117
scope.vm.credentials = 'Bar';
106118

107119
// Test expected POST request

modules/users/tests/e2e/users.e2e.tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Users E2E Tests:', function () {
113113
expect(element.all(by.css('.error-text')).get(0).getText()).toBe('Email address is invalid.');
114114
});
115115

116-
it('Should report missing username', function () {
116+
it('Should report missing username or email', function () {
117117
browser.get('http://localhost:3001/authentication/signup');
118118
// Enter First Name
119119
element(by.model('vm.credentials.firstName')).sendKeys(user1.firstName);
@@ -306,7 +306,7 @@ describe('Users E2E Tests:', function () {
306306
// Click Submit button
307307
element(by.css('button[type="submit"]')).click();
308308
// Username Error
309-
expect(element.all(by.css('.error-text')).get(0).getText()).toBe('Username is required.');
309+
expect(element.all(by.css('.error-text')).get(0).getText()).toBe('Username or Email is required.');
310310
// Password Error
311311
expect(element.all(by.css('.error-text')).get(1).getText()).toBe('Password is required.');
312312
});
@@ -317,7 +317,7 @@ describe('Users E2E Tests:', function () {
317317
// Sign in
318318
browser.get('http://localhost:3001/authentication/signin');
319319
// Enter UserName
320-
element(by.model('vm.credentials.username')).sendKeys(user1.username);
320+
element(by.model('vm.credentials.usernameOrEmail')).sendKeys(user1.username);
321321
// Enter Password
322322
element(by.model('vm.credentials.password')).sendKeys(user1.password);
323323
// Click Submit button

modules/users/tests/server/user.server.routes.tests.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var semver = require('semver'),
1414
var app,
1515
agent,
1616
credentials,
17+
credentialsEmail,
1718
user,
1819
_user,
1920
admin;
@@ -32,9 +33,15 @@ describe('User CRUD tests', function () {
3233
});
3334

3435
beforeEach(function (done) {
35-
// Create user credentials
36+
// Create user credentials with username
3637
credentials = {
37-
username: 'username',
38+
usernameOrEmail: 'username',
39+
password: '[email protected]$Aw3$0m3'
40+
};
41+
42+
// Create user credentials with email
43+
credentialsEmail = {
44+
usernameOrEmail: '[email protected]',
3845
password: '[email protected]$Aw3$0m3'
3946
};
4047

@@ -44,7 +51,7 @@ describe('User CRUD tests', function () {
4451
lastName: 'Name',
4552
displayName: 'Full Name',
4653
47-
username: credentials.username,
54+
username: credentials.usernameOrEmail,
4855
password: credentials.password,
4956
provider: 'local'
5057
};
@@ -83,7 +90,8 @@ describe('User CRUD tests', function () {
8390
});
8491
});
8592

86-
it('should be able to login successfully and logout successfully', function (done) {
93+
it('should be able to login with username and email successfully and logout successfully', function (done) {
94+
// Login with username
8795
agent.post('/api/auth/signin')
8896
.send(credentials)
8997
.expect(200)
@@ -111,7 +119,18 @@ describe('User CRUD tests', function () {
111119
signoutRes.text.should.equal('Moved Temporarily. Redirecting to /');
112120
}
113121

114-
return done();
122+
// Login with username
123+
agent.post('/api/auth/signin')
124+
.send(credentials)
125+
.expect(200)
126+
.end(function (signinErr, signinRes) {
127+
// Handle signin error
128+
if (signinErr) {
129+
return done(signinErr);
130+
}
131+
132+
return done();
133+
});
115134
});
116135
});
117136
});
@@ -711,11 +730,11 @@ describe('User CRUD tests', function () {
711730
_user2.email = '[email protected]';
712731

713732
var credentials2 = {
714-
username: 'username2',
733+
usernameOrEmail: 'username2',
715734
password: '[email protected]$Aw3$0m3'
716735
};
717736

718-
_user2.username = credentials2.username;
737+
_user2.username = credentials2.usernameOrEmail;
719738
_user2.password = credentials2.password;
720739

721740
var user2 = new User(_user2);
@@ -763,11 +782,11 @@ describe('User CRUD tests', function () {
763782
_user2.email = '[email protected]';
764783

765784
var credentials2 = {
766-
username: 'username2',
785+
usernameOrEmail: 'username2',
767786
password: '[email protected]$Aw3$0m3'
768787
};
769788

770-
_user2.username = credentials2.username;
789+
_user2.username = credentials2.usernameOrEmail;
771790
_user2.password = credentials2.password;
772791

773792
var user2 = new User(_user2);

0 commit comments

Comments
 (0)