generator-sails-rest-api
Version:
Yeoman generator that provides already configured and optimized Sails REST API with bundle of predefined features
83 lines (66 loc) • 1.39 kB
JavaScript
;
/**
* User
* @description :: Model for storing users
*/
module.exports = {
schema: true,
attributes: {
username: {
type: 'string',
required: true,
unique: true,
alphanumericdashed: true
},
password: {
type: 'string'
},
email: {
type: 'email',
required: true,
unique: true
},
firstName: {
type: 'string',
defaultsTo: ''
},
lastName: {
type: 'string',
defaultsTo: ''
},
photo: {
type: 'string',
defaultsTo: '',
url: true
},
socialProfiles: {
type: 'object',
defaultsTo: {}
},
toJSON() {
let obj = this.toObject();
delete obj.password;
delete obj.socialProfiles;
return obj;
}
},
beforeUpdate(values, next) {
if (false === values.hasOwnProperty('password')) return next();
if (/^\$2[aby]\$[0-9]{2}\$.{53}$/.test(values.password)) return next();
return HashService.bcrypt.hash(values.password)
.then(hash => {
values.password = hash;
next();
})
.catch(next);
},
beforeCreate(values, next) {
if (false === values.hasOwnProperty('password')) return next();
return HashService.bcrypt.hash(values.password)
.then(hash => {
values.password = hash;
next();
})
.catch(next);
}
};