Authentication and Authorization 2- Registering a new User

On postman I’m trying to post “_id”, “name” and “email” properties on my http://localhost:3900/api/users, but I keep getting
“500 internal server error.”

Is anyone else having this problem? And what did I do wrong???

here’s my users.js code:

const auth = require("…/middleware/auth");

const bcrypt = require(“bcrypt-nodejs”);

const _ = require(“lodash”);

const { User, validate } = require("…/models/user");

const express = require(“express”);

const router = express.Router();

router.get("/me", auth, async (req, res) => {

const user = await User.findById(req.user._id).select("-password");

res.send(user);

});

router.post("/", async (req, res) => {

const { error } = validate(req.body);

if (error) return res.status(400).send(error.details[0].message);

let user = await User.findOne({ email: req.body.email });

if (user) return res.status(400).send(“User already registered.”);

user = new User(_.pick(req.body, [“name”, “email”, “password”]));

const salt = await bcrypt.genSalt(10);

user.password = await bcrypt.hash(user.password, salt);

await user.save();

const token = user.generateAuthToken();

res

.header("x-auth-token", token)

.send(_.pick(user, ["_id", "name", "email"]));

});

module.exports = router;

Okay I think I figured it out. bcrypt-nodejs is no longer active https://www.npmjs.com/package/bcrypt-nodejs. Instead use bcryptjs https://www.npmjs.com/package/bcrypt.

Plus I upgraded my genSalt and hash code:

 bcrypt.genSalt(10, function (_err, salt) {

       
        bcrypt.hash(user.name, salt, function (_err, hash) {

       // Store hash in your password DB.

       user.password = hash;

     });

  });