Sequelize doesn't hash password with bcrypt

163 views
Skip to first unread message

filipec...@gmail.com

unread,
May 14, 2017, 5:37:08 AM5/14/17
to Sequelize

I am using nodejs + postgreSQL to do the registration of my app, so i have this basic model:

"use strict"; var sequelize = require('./index'); var bcrypt = require('bcryptjs'); module.exports = function (sequelize, DataTypes) { var User = sequelize.define("User", { username: DataTypes.STRING, email: DataTypes.STRING, password: DataTypes.STRING }, { instanceMethods: { generateHash: function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }, validPassword: function(password) { return bcrypt.compareSync(password, this.password); }, } }); return User; };

it worked before when i didn't had the password, now i want to hash that password and salt, so in my register route i have this:

 var express = require('express'); var User = require('../../models').User; var router = express.Router(); /* GET users listing. */ router.post('/', function (req, res, next) { if (JSON.stringify(req.body) == "{}") { return res.status(400).json({ Error: "Register request body is empty" }); } if (!req.body.email || !req.body.username || !req.body.password) { return res.status(400).json({ Error: "Missing fields for registration" }); } var password = User.generateHash(req.body.password); User.create({ username: req.body.username, email: req.body.email, password: password }).then(function () { return res.status(200).json({message: "user created"}); }) }); module.exports = router;

Reply all
Reply to author
Forward
0 new messages