I am trying to create a login end point and sending auth token but when i am sending request for creating user it works fine but when i send request for login authentication it is sending response as internal server error and in console it is sending me this error:-
Illegal arguments: string, undefined
[nodemon] restarting due to changes...
[nodemon] starting `node server.js index.js`
at _async (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcryptjs\dist\bcrypt.js:286:46) at D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcryptjs\dist\bcrypt.js:307:17
at new Promise (<anonymous>)
at Object.bcrypt.compare (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcryptjs\dist\bcrypt.js:306:20)
7:17 at new Promise (<anonymous>)
at Object.bcrypt.compare (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcryptjs\dist\bcrypt.js:306:20)
at D:\Web Development\REACT PROJECTS\inotebook\backend\routes\auth.js:73:42 at processTicksAndRejections (node:internal/process/task_queues:96:5)
Here is my complete code of auth.js:-
const express = require('express');
const User = require('../models/User');
const router = express.Router();
const { body, validationResult } = require('express-validator');
const bcrypt = require('bcryptjs');
var jwt = require('jsonwebtoken');
const JWT_SECRET = 'usisagoodb$oy';
// Create a User using: POST "/api/auth/createuser". No login required
router.post('/createuser', [
body('name', 'Enter a valid name').isLength({ min: 3 }),
body('email', 'Enter a valid email').isEmail(),
body('password', 'Password must be atleast 5 characters').isLength({ min: 5 }),
], async (req, res) => {
// If there are errors, return Bad request and the errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
// Check whether the user with this email exists already
let user = await User.findOne({ email: req.body.email });
if (user) {
return res.status(400).json({ error: "Sorry a user with this email already exists" })
}
const salt = await bcrypt.genSalt(10);
const secPass = await bcrypt.hash(req.body.password, salt);
// Create a new user
user = await User.create({
name: req.body.name,
password: secPass,
email: req.body.email,
});
const data = {
user:{
id: user.id
}
}
const authtoken = jwt.sign(data, JWT_SECRET);
// res.json(user)
res.json({authtoken})
} catch (error) {
console.error(error.message);
res.status(500).send("Internal Server Error");
}
})
// Authenticate a User using: POST "/api/auth/login". No login required
router.post('/login', [
body('email', 'Enter a valid email').isEmail(),
body('password', 'Password cannot be blank').exists(),
], async (req, res) => {
// If there are errors, return Bad request and the errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const {email, password} = req.body;
try {
let user = await User.findOne({email});
if(!user){
return res.status(400).json({error: "Please try to login with correct credentials"});
}
const passwordCompare = await bcrypt.compare(password, user.password);
if(!passwordCompare){
return res.status(400).json({error: "Please try to login with correct credentials"});
}
const data = {
user:{
id: user.id
}
}
const authtoken = jwt.sign(data, JWT_SECRET);
res.json({authtoken})
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
})
module.exports = router
i also tried to replace const bcrypt = require('bcryptjs'); with const bcrypt = require('bcrypt');
but this also gave me this error:-
Error: data and hash arguments required at Object.compare (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcrypt\bcrypt.js:208:17)
at D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcrypt\promises.js:29:12
at new Promise (<anonymous>)
at Object.module.exports.promise (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcrypt\promises.js:20:12)
at Object.compare (D:\Web Development\REACT PROJECTS\inotebook\backend\node_modules\bcrypt\bcrypt.js:204:25)
at D:\Web Development\REACT PROJECTS\inotebook\backend\routes\auth.js:73:42
at processTicksAndRejections (node:internal/process/task_queues:96:5)
MAINLY IT IS GIVING ME ERROR ON THIS PART OF CODE WHEN I AM USING bcrypt.compare function
const {email, password} = req.body;
try {
let user = await User.findOne({email});
if(!user){
return res.status(400).json({error: "Please try to login with correct credentials"});
}
const passwordCompare = await bcrypt.compare(password, user.password);
if(!passwordCompare){
return res.status(400).json({error: "Please try to login with correct credentials"});
}
CodePudding user response:
The only thing that looks out of place is
let user = await User.findOne({email});
it should be like:
let user = await User.findOne({where:{email}});
as you can also see in the documentation
CodePudding user response:
const userEmail=req.body.email;
const userPassword=req.body.password;
let user = await User.findOne({email:userEmail});
use bcrypt instead of bcryptjs