2015-08-23 11 views
5

Voglio salvare una password con hash. Sto utilizzando un setterMethod per questo:Come posso eseguire le convalide del modello prima di setterMethod in Sequelize?

module.exports = (sequelize, DataTypes) -> 
    sequelize.define 'User', 
    # other model fields [...] 
    password: 
     type: DataTypes.STRING 
     validate: notEmpty: msg: 'You need to set a password.' 
     set: (pw) -> 
     salt = bcrypt.genSaltSync(10) 
     hash = bcrypt.hashSync(pw, salt) 
     @setDataValue('password', hash) 

Il setter viene eseguito per primo. Una password di stringa vuota ('') è sottoposta a hash in una non vuota (ad esempio $2a$10$pDDIGnV.r47i9YOv0Fls/euQ0yYvfyq8T1SyP9VRQsTUAqptNmxXO).

Quando il validatore convalida, la password non è più vuota.

Come posso convalidare la password prima del setter?

Ho esaminato hooks ma non menzionano neanche gli setter.

Sto usando [email protected].

risposta

0

Ho risolto questo problema utilizzando due campi, uno dei quali è il tipo VIRTUAL che gestisce l'input e la convalida e uno è il tipo STRING che contiene la password con hash.

Questo esempio non è coffeescript ma dovresti essere in grado di tradurre facilmente.

password_hash: { 
    type: DatabaseTypes.STRING, 
    allowNull: false, 
    validate: { 
    notEmpty: true, 
    }, 
}, 
password: { 
    type: DatabaseTypes.VIRTUAL, 
    allowNull: false, 
    // note that arrow functions cannot access "this", so use the form: 
    set: function setPassword(val) { 
    // trigger validation on "password" field 
    this.setDataValue('password', val); 

    // hash the password, this can be done in one step by passing the 
    // number of salt rounds instead of the salt string. 
    this.setDataValue('password_hash', bcrypt.hashSync(val, 10)); 
    }, 
    validate: { 
    notEmpty: { 
     message: 'You need to set a password.', 
    }, 
    }, 
}, 

Quando si autenticare l'utente confrontare la password immessa per User.password_hash piuttosto che User.password.

instanceMethods: { 
    // authenticate user given a password 
    authenticate(password) { 
    return bcrypt.compareSync(password, this.password_hash); 
    }, 
}, 

È quindi possibile chiamare questo metodo di istanza per autenticare un User.

User.findById(userId) 
.then((user) => { 
    if (user.authenticate(password)) { 
    console.log('Authenticated'); 
    } else { 
    console.log('Not authenticated'); 
    } 
}); 
Problemi correlati