Im trying to create simple login app with MERN stack and i have an issue while using bcrypt to hash my password.
server.js
const mongoose = require("mongoose");
const express = require("express");
const cors = require("cors");
const passport = require("passport");
const passportLocal = require("passport-local").Strategy;
const cookieParser = require("cookie-parser");
const bcrypt = require("bcrypt");
const session = require("express-session");
const bodyParser = require("body-parser");
const app = express();
const User = require("./user")
mongoose.connect(
mongodbLink,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
() => {
console.log("Mongoose Is Connected");
}
);
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
cors({
origin: "http://localhost:3000", // <-- location of the react app were connecting to
credentials: true,
})
);
app.use(
session({
secret: "secretcode",
resave: true,
saveUninitialized: true,
})
);
app.use(cookieParser("secretcode"));
app.use(passport.initialize());
app.use(passport.session());
//routes
app.post('/login',(req,res,next) => {
passport.authenticate("local",(err,user,info) =>{
if (err) throw err
if(!user) res.send("No user with given login")
else {
req.logIn(user, (err) => {
if (err) throw err
res.send("Succesfully Authenticated")
console.log(req.user)
})
}
})(req,res,next)
})
app.post("/register", (req, res) => {
User.findOne({ username: req.body.username }, async (err, doc) => {
if (err) throw err;
if (doc) res.send("User Already Exists");
if (!doc) {
const hashedPassword = bcrypt.hashSync(req.body.password, 10);
const newUser = new User({
username: req.body.username,
password: hashedPassword,
});
await newUser.save();
res.send("User Created");
}
});
});
app.get('/user',(req,res) => {
res.send(req.user)
})
//listen
app.listen(4000, () => {
console.log("Server Has Started");
});
User.js
const user = new mongoose.Schema({
username: String,
password: String,
})
module.exports = mongoose.model("User",user)
My postman post request on http://localhost:4000/register
{
"username":"user123",
"password":"pass",
}
I have this error message in console when im testing my register route using postman and i cant get any response.
(node:5452) UnhandledPromiseRejectionWarning: Error: data and salt arguments required
at Object.hashSync (C:UsersPCDesktopprojectsmern_loginbackendnode_modulesbcryptbcrypt.js:91:15)
at C:UsersPCDesktopprojectsmern_loginbackendserver.js:64:39
at C:UsersPCDesktopprojectsmern_loginbackendnode_modulesmongooselibmodel.js:4866:16
at C:UsersPCDesktopprojectsmern_loginbackendnode_modulesmongooselibmodel.js:4866:16
at C:UsersPCDesktopprojectsmern_loginbackendnode_modulesmongooselibhelperspromiseOrCallback.js:24:16
at C:UsersPCDesktopprojectsmern_loginbackendnode_modulesmongooselibmodel.js:4889:21
at C:UsersPCDesktopprojectsmern_loginbackendnode_modulesmongooselibquery.js:4399:11
at C:UsersPCDesktopprojectsmern_loginbackendnode_moduleskareemindex.js:136:16
at processTicksAndRejections (internal/process/task_queues.js:79:11)
(node:5452) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5452) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I don’t know what am i doing wrong im simply trying to asynchronously call hash method to hash my password.
Looks like req.body.password
is undefined
causing the error.
Check if the password is being sent in the request body.