Saturday, 13 May 2017

cannot register user with bcrypt postgreSQL

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;

now i just don't get any response of my postman and the row isn't saving on the databse any sugestion?



via Antonio Costa

No comments:

Post a Comment