I am doing the following thing in node js
1.register and save user in mongodb 2.generate token when registered. 3.authorization using token 4.login user and
this is my router file
const express = require('express'); const router = express.Router(); const User = require('../models/user'); const bcrypt= require('bcryptjs'); const use_jwt = require('../middleware/use_jwt.js') const jwt = require('jsonwebtoken'); const user = require('../models/user'); //const user = require('../models/user'); const env = require ('dotenv').config(); router.get('/',use_jwt , async(req,res,next)=>{ try{ const user_id = req.user.id; const user2 = await User.findById(user_id).select('-password'); res.status(200).json({ success:true, user:user2 }) } catch(err){ console.log(err); res.status(401).json({ msg:"server not responding" }) } next(); }) router.post('/register',async(req,res,next)=>{ const {username,email,password}=req.body; try{ let user_exist = await User.findOne({email:email}); if(user_exist){ return res.status(401).json({ "success":"false", "msg":"user already exist" }) } //importing data to models const users = new User(); users.username = username; users.email = email ; console.log(users.email); console.log(password) const salt = await bcrypt.genSalt(10); users.avatar = "https://gravatar.com/avatar/?s=200&d=retro" users.password = await bcrypt.hash(password,salt) console.log(users.password); console.log("hello"); await users.save(); const payload = { user :{ id:users.id } } console.log(payload.user.id); jwt.sign(payload,process.env.jwtUsersecret,{ expiresIn : 3600000 },(err,token)=>{ if(err) throw err; else { res.status(200).json({ success:true, token:token }) } }) next(); } catch(err){ console.log(err); } }); router.post('/login',async (req,res,next)=>{ const email = req.body.email const password = req.body.password console.log('hello2') console.log(password) console.log('hello1') try{ const user_exist = User.findOne({email:email}) if(!user_exist){ console.log('one user tried to login without registering') res.status(400).json({ "msg":"pls register and then try to login because there is no such user", "sucess":"false" }) } //console.log(user) bcrypt.compare(password, users.password, (err, res) => { if (err) throw err; if (res) {(req,res)=>{ const payload = { user: { id: users.id } } console.log(payload.user.id); token = jwt.sign(payload,process.env.jwtUsersecret) res.status(200).json({ "sucess": "true", "user": user, "email": email, "token": token }); } } else { res.json({ "success": "false", "msg": "password incorrect" }); } }) } catch(err){ console.log(err); res.status(400).json({ "msg":"server not responding" }) } }) module.exports = router;
but when i give post request . It says users is not defined at
bcrypt.compare(password,users.password)
pls find an solution for this . I am sorry if it is a silly or bad question . thanks
Advertisement
Answer
You didn’t identify “Users” so you can chick as:
User.findOne({email: email}, (err, user) > { > if(err) throw err; > if(!user){ > console.log("User dose not exisit") > } > bcrypt.compare(password, user.password , (err, result) => { > if (err) throw err; > if (result === true) { > //User Is Found and password match > } else { > //User Password dose not match > } > }); > > })
//It will store found user and chick if user email existed if yes it will chick password
I hope that helps 😊