-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer.js
63 lines (53 loc) · 1.94 KB
/
customer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const passAlreadyHashed = require('../../utils/pass-already-hashed');
const throwResponseError = require('../../utils/throw-response-error');
module.exports = function(Customer) {
const {min: PWD_MIN, max: PWD_MAX} = Customer.definition.properties.password;
/**
* The standard validation is not valid, because uses the hashed string instead of the plain password:
*
* Customer.validatesLengthOf('password', {
* min: 6,
* message: { min: 'Password is too short' }
* });
*
* We've to override the built-in function of common/models/user.js:
*/
Customer.validatePassword = function(plain) {
if (!plain || typeof plain !== 'string') {
throwResponseError(422, 'invalid', 'password', 'Invalid password');
}
const len = Buffer.byteLength(plain, 'utf8');
if (len > PWD_MAX) {
throwResponseError(422, 'tooLong', 'password', `The password entered was too long. Max length is ${PWD_MAX} (entered ${len})`);
}
if (len < PWD_MIN) {
throwResponseError(422, 'tooShort', 'password', `The password entered was too short. Min length is ${PWD_MIN} (entered ${len})`);
}
};
/**
* The built-in function of common/models/user.js only checks the hashes starting with $2a$:
* https://github.com/strongloop/loopback/blob/master/common/models/user.js#L1092
*
* We've to override the built-in function to check the hash properly:
*/
Customer.setter.password = function(plain) {
if (typeof plain !== 'string') {
return;
}
if (passAlreadyHashed(plain)) {
// The password is already hashed. It may be the case when the instance is loaded from DB
this.$password = plain;
} else {
this.$password = this.constructor.hashPassword(plain);
}
};
/**
* Example of Remote Method
*/
Customer.hello = function(msg, cb) {
process.nextTick(function() {
cb(null, 'Sender says ' + (msg || 'hello') + ' to receiver');
});
};
};