i have this segment of code that does all the login and register stuff on my website. I firstly developed in another folder, and there everything worked, but after importing it into my main site, it stopped working.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="POST" action="/register-new" style = "display:flex; flex-direction: column; align-items:center; justify-content: center; height:100vh">
<input name = "username" placeholder = "username" type="text" required>
<input name = "password" placeholder = "password" type="text" required>
<button type = "submit"> Submit</button>
</form>
</body>
</html>
Express.js code
var express = require('express');
let router = express.Router();
let userForm = require("./postAccount")
const mongoose = require("mongoose");
let session = require('express-session');
var Sequelize = require('sequelize');
const bcrypt = require('bcrypt');
router.use(express.json());
function checkUniqueRegister(data,res,req){
let promise = new Promise((suc, fai)=>{
userForm.findOne({ username : data.username }, function (err, user) {
if(user === null){
userForm.create(data)
res.redirect("/account/accountpage")
suc()
}else{
res.redirect("/fail")
fai()
}
});
})
return promise
}
function checkUniqueLogin(data, res){
let promise = new Promise((suc, fai)=>{
userForm.findOne({ username : data.username}, function (err, user) {
if(user === null){
res.redirect("/fail")
fai()
}else{
if (bcrypt.compareSync(data.password,user.passwordHash)){
res.redirect("/account/accountpage")
suc()
}
}
});
})
return promise
}
router.get('/register',function(req,res){
res.render('../view/register',{errors: ""});
});
router.get('/login',function(req,res){
res.render('../view/login',{errors: ""});
});
router.get('/account/accountpage',function(req,res){
const user = req.session.username
res.render('accountpage',{username: user});
});
router.post('/register-new', async (req,res) => {
const passwordHash = bcrypt.hashSync(req.body.password,10);
const {username, password} = req.body
checkUniqueRegister({username,password,passwordHash},res,req).then(()=>{let newSession = req.session;newSession.username = username;req.session.save();}, ()=>{console.log("fail")})
})
router.post('/login-user', async (req,res) => {
const passwordHash = bcrypt.hashSync(req.body.password,10);
const {username,password} = req.body
checkUniqueLogin({username, password, passwordHash},res).then(()=>{let newSession = req.session;newSession.username = username;req.session.save();}, ()=>{console.log("fail")})
})
router.get("/all-items/delete", async (req,res)=>{
await userForm.deleteMany()
res.json({"message": "all data has been deleted"})
})
module.exports = router
Error code
C:\xampp\htdocs\node_modules\bcrypt\bcrypt.js:91
throw new Error('data and salt arguments required');
^
Error: data and salt arguments required
at Object.hashSync (C:\xampp\htdocs\node_modules\bcrypt\bcrypt.js:91:15)
at C:\xampp\htdocs\serverSide\routerAccount.js:58:33
at Layer.handle [as handle_request] (C:\xampp\htdocs\node_modules\express\lib\router\layer.js:95:5)
at next (C:\xampp\htdocs\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\xampp\htdocs\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\xampp\htdocs\node_modules\express\lib\router\layer.js:95:5)
at C:\xampp\htdocs\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\xampp\htdocs\node_modules\express\lib\router\index.js:346:12)
at next (C:\xampp\htdocs\node_modules\express\lib\router\index.js:280:10)
at jsonParser (C:\xampp\htdocs\node_modules\body-parser\lib\types\json.js:119:7)
Also the req.body()
return {}
, i belive that there might be some problems with the path but i can't see them, also the code line that crashes my app is this one, althought it worked great before i imported all of the code. bcrypt.compareSync(data.password,user.passwordHash)
. The whole thing comes from the empty req.body()
CodePudding user response:
You need to add
router.use(express.urlencoded({ extended: true }));
after the router.use(express.json());
You can learn more about that function in the official docs, right here, http://expressjs.com/en/api.html#express.urlencoded.
Why you should need that ?
Basically, when you send a request over a form with action="POST"
, it sends a request with the HTTP header Content-Type: application/x-www-form-urlencoded
.
Since express.json()
handles the Content-Type: application/json
bodies, the express.urlencoded()
one handles the application/x-www-form-urlencoded
bodies.
CodePudding user response:
The error comes from the bcrypt.hashSync method. In your case, you have the following piece of code :
const passwordHash = bcrypt.hashSync(req.body.password,10);
I think that your problem comes from the req.body.password that must be empty (null or undefined). The error says data and salt arguments are required. It looks like your salt is correctly generated and you didn't check if req.body.password === undefined, somehow req.body.password is undefined.