0

problem: i keep getting the error in title when trying to register a new user only when using postman no idea why.

Also, Proxy problems: Could not proxy request /api/auth/register?hello&world from localhost:3000 to http://localhost:8080/.

Things i've tried to solve proxy was adding "secure": false in package json.

my auth.js file:

exports.postRegister = async (req, res, next) => {
  // User object
  const newUser = {
    username: req.body.username,
    password: req.body.password,
  };
  // Check if user exists
  try {
    const user = await User.findOne({ username: newUser.username });
    if (user) {
      const error = new Error("Username already taken.");
      error.statusCode = 422;
      return next(error);
    }
  } catch (error) {
    error.statusCode = error.statusCode || 500;
    next(error);
  }

  // creating a hash from the given password
  bcrypt.genSalt(12, (err, salt) => {
    if (err) {
      return next(err);
    }
    bcrypt.hash(newUser.password, salt, null, async (err, hash) => {
      if (err) {
        return next(err);
      }
      try {
        // set hashed password as the password field in newUser object.
        newUser.password = hash;

        // Save newUser
        const createdUser = new User(newUser);
        await createdUser.save();

        // Create jwt token and send
        const token = await genToken(createdUser._id, createdUser.username);
        res.status(201).json({
          success: true,
          token,
        });
      } catch (err) {
        err.statusCode = err.statusCode || 500;
        next(err);
      }
    });
  });
};

Server.js file:

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const helmet = require("helmet");
const cors = require("cors");

require("dotenv").config();

const mailRoutes = require("./routes/mail");
const authRoutes = require("./routes/auth");

const app = express();

app.use(bodyParser.json());
app.use(helmet());
app.use(cors());

// Routes
app.use("/api/mail", mailRoutes);
app.use("/api/auth", authRoutes);

app.use((error, req, res, next) => {
  console.log(error.message);
  res.status(error.statusCode).json({
    error: true,
    messages: [error.message],
  });
});

mongoose.connect(process.env.DB_URI, (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log("DB Connected");
  }
});

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log(`Server has started at ${PORT}`);
});
92yo
  • 518
  • 1
  • 4
  • 12
  • try to console.log what values of password and salt passed to bcrypt. – Medi Jul 08 '20 at 13:34
  • salt is returning bunch of random giberish which means its working same goes for password its passing the correct user input – 92yo Jul 08 '20 at 14:13
  • 1
    But the error says the opposite. It says data and salt are required, which means you arent passing them to bcrypt. – Medi Jul 08 '20 at 14:16
  • so apprently console logging inside of the bcrypt.hash is not logging so i guess its not accessing it that might be my problem. any ideas? – 92yo Jul 08 '20 at 14:32

1 Answers1

0

Ok so to anyone who is facing the same problem - i figured it out. changing from arrow functions to regular function. apparently bcrypt doesnt accept arrow functions

so instead of:

bcrypt.hash(newUser.password, salt, async => (err, hash){}...

do this:

bcrypt.hash(newUser.password, salt, async function (err, hash){}...
92yo
  • 518
  • 1
  • 4
  • 12