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

Commit df32656

Browse files
authored
Merge pull request #23 from adamvr/entity-fields
Entity fields
2 parents 460e64c + 338c2fd commit df32656

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ This will pull from your global `auth` object in your config file. It will also
4848
name: 'local', // the name to use when invoking the authentication Strategy
4949
entity: 'user', // the entity that you're comparing username/password against
5050
service: 'users', // the service to look up the entity
51-
usernameField: 'email', // key name of username field
52-
passwordField: 'password', // key name of password field
51+
usernameField: 'email', // key name of username field on the request
52+
passwordField: 'password', // key name of password field on the request
53+
entityUsernameField: 'email', // key name of the username field on the entity (defaults to `usernameField`)
54+
entityPasswordField: 'password', // key name of the password on the entity (defaults to `passwordField`)
5355
passReqToCallback: true, // whether the request object should be passed to `verify`
5456
session: false // whether to use sessions,
5557
Verifier: Verifier // A Verifier class. Defaults to the built-in one but can be a custom one. See below for details.

src/verifier.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ class LocalVerifier {
2121
}
2222

2323
_comparePassword(entity, password) {
24+
// select entity password field - take entityPasswordField over passwordField
25+
const passwordField = this.options.entityPasswordField || this.options.passwordField;
26+
2427
// find password in entity, this allows for dot notation
25-
const hash = get(entity, this.options.passwordField);
28+
const hash = get(entity, passwordField);
2629

2730
if (!hash) {
28-
return Promise.reject(new Error(`'${this.options.entity}' record in the database is missing a '${this.options.passwordField}'`));
31+
return Promise.reject(new Error(`'${this.options.entity}' record in the database is missing a '${passwordField}'`));
2932
}
3033

3134
debug('Verifying password');
@@ -64,8 +67,12 @@ class LocalVerifier {
6467

6568
verify(req, username, password, done) {
6669
debug('Checking credentials', username, password);
70+
71+
// Choose username field
72+
const usernameField = this.options.entityUsernameField || this.options.usernameField;
73+
6774
const query = {
68-
[this.options.usernameField]: username,
75+
[usernameField]: username,
6976
$limit: 1
7077
};
7178

test/verifier.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ describe('Verifier', () => {
109109
expect(result).to.deep.equal(user);
110110
});
111111
});
112+
113+
it('prefers entityPasswordField over passwordField', () => {
114+
user.password = {
115+
value: user.password
116+
};
117+
118+
verifier.options.passwordField = 'password';
119+
verifier.options.entityPasswordField = 'password.value';
120+
121+
return verifier._comparePassword(user, 'admin').then(result => {
122+
expect(result).to.deep.equal(user);
123+
});
124+
})
112125
});
113126
});
114127

@@ -152,6 +165,36 @@ describe('Verifier', () => {
152165
});
153166
});
154167

168+
it('allows overriding of usernameField', done => {
169+
verifier.options.usernameField = 'username';
170+
171+
user.username = 'username';
172+
173+
verifier.verify({}, 'username', 'admin', (error, entity) => {
174+
expect(error).to.equal(null);
175+
expect(entity).to.deep.equal(user);
176+
done();
177+
})
178+
});
179+
180+
it('prefers entityUsernameField over usernameField', done => {
181+
verifier.options.usernameField = 'username';
182+
verifier.options.entityUsernameField = 'users.username';
183+
184+
user.username = 'invalid';
185+
186+
user.users = {
187+
username: 'valid'
188+
};
189+
190+
verifier.verify({}, 'valid', 'admin', (error, entity) => {
191+
expect(error).to.equal(null);
192+
expect(entity).to.deep.equal(user);
193+
done();
194+
})
195+
196+
});
197+
155198
it('calls _normalizeResult', done => {
156199
sinon.spy(verifier, '_normalizeResult');
157200
verifier.verify({}, user.email, 'admin', () => {

0 commit comments

Comments
 (0)