# Authentication and Authorization 2- Registering a new User

**URL:** <https://forum.codewithmosh.com/t/authentication-and-authorization-2-registering-a-new-user/1328>\
**Category:** React\
**Created:** [December 19, 2020, 12:10am UTC](https://forum.codewithmosh.com/t/authentication-and-authorization-2-registering-a-new-user/1328 "2020-12-19T00:10:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfabbi](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pfabbi](https://forum.codewithmosh.com/u/pfabbi)\
**Post date:** [December 19, 2020, 12:10am UTC](https://forum.codewithmosh.com/t/authentication-and-authorization-2-registering-a-new-user/1328/1 "2020-12-19T00:10:33Z")

</div>

On postman I’m trying to post “\_id”, “name” and “email” properties on my [http://localhost:3900/api/users](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;

---

<div class="post-metadata">

**Author:** ![pfabbi](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pfabbi](https://forum.codewithmosh.com/u/pfabbi)\
**Post date:** [December 29, 2020, 12:30am UTC](https://forum.codewithmosh.com/t/authentication-and-authorization-2-registering-a-new-user/1328/2 "2020-12-29T00:30:47Z")

</div>

Okay I think I figured it out. bcrypt-nodejs is no longer active [https://www.npmjs.com/package/bcrypt-nodejs](https://www.npmjs.com/package/bcrypt-nodejs). Instead use bcryptjs [https://www.npmjs.com/package/bcrypt](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;

     });

  });
```

---

<div class="post-metadata">

**Author:** ![Mdchristien](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/mdchristien/32/5930_2.png) [@Mdchristien](https://forum.codewithmosh.com/u/Mdchristien)\
**Post date:** [September 26, 2023, 10:59am UTC](https://forum.codewithmosh.com/t/authentication-and-authorization-2-registering-a-new-user/1328/3 "2023-09-26T10:59:04Z")

</div>

thanks so much bro , ur solution helped me
