Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // api/models
- import bcrypt from 'bcrypt';
- module.exports = {
- attributes: {
- username: {
- type: 'string',
- required: true,
- unique: true,
- },
- email: {
- type: 'string',
- required: true,
- unique: true,
- isEmail: true,
- },
- password: {
- type: 'string',
- required: true,
- },
- },
- customToJSON: function() {
- return _.omit(this, ['password']);
- },
- // Lifecycle
- beforeCreate: function(values, next) {
- if (values.password) {
- bcrypt.genSalt(10, function (err, salt) {
- if (err) return next(err);
- bcrypt.hash(values.password, salt, function (err, hash) {
- if (err) return next(err);
- values.password = hash;
- next();
- });
- });
- } else {
- next();
- }
- },
- };
Add Comment
Please, Sign In to add comment