EQuimper / nodejs-api-boilerplate

A boilerplate for kickstart your nodejs api project with JWT Auth and some new Techs :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

yarn db:seeds-user

opensourcekam opened this issue · comments

I'm lead to believe the way we're seeding the db with users doesn't create a BCrypt hash correctly. I was stuck on this for almost 20 minutes trying to figure out why I couldn't login with one of the seeded users information. With a bit of further investigation in user.model.js I noticed each user is correctly being created using hashSync & logged in using compareSync.. Perfect so no issues there I was stumped... So I finally read through user.seed.js and noticed you're doing this

     await User.insertMany(users);

which I think could be perfect in the case that I have a UI setup to fetch and display all users but If I needed to seed the db with accounts that would allow me to login this would be an issue. This method also avoids the middleware stack that you're using in

   routes.post(
     '/signup',
     validate(UserController.validation.create),
     UserController.create,
   );

the solution that I came with is very simple I think. Instead of the direct insert I added request as a devDependency so you can do

     return users.forEach((user) => {
    post(
      `http://localhost:${process.env.PORT || 3000}/api/users/signup`,
      { json: user },
      (error, response, body) => {
        if (!error && response.statusCode === 200) {
          console.log(body);
        }
      },
    );
  });

this method was successful for signup and i've tested it for login and it was successful!

My logs are below! Let me know what you think. If you like my idea I can sub a PR! Thanks for your time.

[dev.start]
[dev.start] {
[dev.start]   "res": {
[dev.start]     "statusCode": 500,
[dev.start]     "body": {
[dev.start]       "message": "Internal Server Error."
[dev.start]     }
[dev.start]   },
[dev.start]   "req": {
[dev.start]     "url": "/api/users/login",
[dev.start]     "headers": {
[dev.start]       "cache-control": "no-cache",
[dev.start]       "postman-token": "f66627d3-2cc7-476c-b7c9-d6722d58fb62",
[dev.start]       "content-type": "application/x-www-form-urlencoded",
[dev.start]       "user-agent": "PostmanRuntime/6.1.6",
[dev.start]       "accept": "*/*",
[dev.start]       "host": "localhost:8081",
[dev.start]       "accept-encoding": "gzip, deflate",
[dev.start]       "content-length": "52",
[dev.start]       "connection": "keep-alive"
[dev.start]     },
[dev.start]     "method": "POST",
[dev.start]     "httpVersion": "1.1",
[dev.start]     "originalUrl": "/api/users/login",
[dev.start]     "query": {},
[dev.start]     "body": {
[dev.start]       "email": "Wilburn.Pacocha@gmail.com",
[dev.start]       "password": "password1"
[dev.start]     }
[dev.start]   },
[dev.start]   "responseTime": 8,
[dev.start]   "level": "info",
[dev.start]   "message": "HTTP POST /api/users/login 500 8ms"
[dev.start] }
[dev.start] POST /api/users/login 500 8.087 ms - 36
[dev.start]   Error: Not a valid BCrypt hash.

Did you finnaly found the issue about the wrong BCrypt hash ... because it seams that you are not hashing your password data in your req.body