Node js mongo db passport problem

hello I am just new in learning about mongo db and node js
but actually I am facing a very bad error
like this is my router users.js

var express = require('express');
var router = express.Router();

const { check, validationResult } = require('express-validator');
const User = require('../models/User');
const passport = require('passport');


/* GET users listing. */
router.get('/signup', function (req, res, next) {
  var massagesError = req.flash('error')
  res.render('signup', { massages: massagesError })
});

router.post('/signup', [
  check('name').not().isEmpty().withMessage('Please Enter Your Name'),


  check('email').not().isEmpty().withMessage('Please Enter Your Email'),
  check('email').isEmail().withMessage('Please Enter Valid Email'),

  check('password').not().isEmpty().withMessage('Please Enter your Password'),
  check('password').isLength({ min: 8 }).withMessage('Please Enter Password more than 8 characters'),
  check('confirm-password').custom((value, { req }) => {
    if (value !== req.body.password) {
      throw new Error('Password and confirm-password do not match');
    }
    return true;
  }),

  check('phoneNumber').not().isEmpty().withMessage('Please Enter your Phone Number'),
  check('phoneNumber').isMobilePhone().withMessage('please enter Valid Phone Numeber'),

  check('userType').isIn(['sponsor', 'student']).withMessage('Please select a valid user type')

], (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {


    var validationMassages = [];
    for (var i = 0; i < errors.errors.length; i++) {
      validationMassages.push(errors.errors[i].msg)
    }

    req.flash('error', validationMassages);
    res.redirect('signup')



    console.log(validationMassages);

    return;
  }
  const user = new User({
    name: req.body.name,
    email: req.body.email,
    password: new User().hashPassword(req.body.password),
    phoneNumber: req.body.phoneNumber,
    userType: req.body.userType

  })

  User.findOne({ email: req.body.email })
    .then(result => {
      if (result) {
        // User with the provided email already exists

        req.flash('error', 'this email already exist')
        res.redirect('signup')
        return;
      }
      user.save()
        .then(doc => {
          res.send(doc);
        })
        .catch(error => {
          console.log(error);
          res.status(500).send('An error occurred while saving the user');
        })
    })





})



router.get('/profile', (req, res, next) => {
  res.render('profile')
});




router.get('/signin', (req, res, next) => {
  res.render('signin');
})

router.post('/signin', passport.authenticate('local-signin', 
{session:false,
  successRedirect: 'profile',
  failureRedirect: 'signin',
  failureFlash : true,
}))

module.exports = router;

and this is my passport file passport.js

const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const User = require('../models/User');

passport.use('local-signin', new localStrategy({
    usernameField: 'email', // default is username, override to accept email
    passwordField: 'password',
    passReqToCallback: true // allows us to access req in the call back
  }, async (req, email, password, done) => {
    // Check if user and password is valid
    let user = await User.findBy('email', email)
    let passwordValid = user && bcrypt.compareSync(password, user.passwordHash)

    // If password valid call done and serialize user.id to req.user property
    if (passwordValid) {
      return done(null, {
        id: user.id
      })
    }
    // If invalid call done with false and flash message
    return done(null, false, {
      message: 'Invalid email and/or password'
    });

}))

every time I try to signin with already existing user in the database the system alwasy redirect me to the failurRedirect even if it correct

hello and welcome to fcc forum :slight_smile:

  • what values do you get for these two

have you tried signing up with a “new user” and then “signing in”? happy coding :slight_smile:

Yes I catually tried registir new user before but the problem still there

normally I use the the the email
test@test.com
and password
123456789

  • whats happening here?!
  • aren’t you supposed to only hash password?
  • what happens when you create a “new user” do you see that been recorded correctly in db?
  • again, what values do you see that for these in console?

happy coding :slight_smile: