美好的一天,我正在使用SQL Server(MSSQL),js节点和通行证进行登录系统,用户注册表可以正常工作,但登录身份验证失败,始终返回未找到的用户,我是新手,我想得到一些指导,请在我进行身份验证的地方附上我的代码,我的代码是:文件passport.js
passport.use('local.signin', new LocalStrategy({
usernameField: 'username',
passwordField: 'passwords',
passReqToCallback: true
}, async (req, username, passwords, done) => {
const request=pool.request();
//request.input('username',username);
//request.input('passwords',passwords);
const rows = await request.query(`SELECT * FROM signUp WHERE username=${username}`);
console.log(passwords)
if (rows.length > 0) {
const user = rows[0];
const validPassword = await helpers.matchPassword(passwords,user.passwords)
if (validPassword) {
done(null, user, req.flash('success', 'Bienvenido' + user.username));
} else {
done(null, false, req.flash('message', 'Incorrect Password'));
}
} else {
return done(null, false, req.flash('message', 'El Nombre de usuario no existe'));
}
}));
passport.use('local.signup', new LocalStrategy({
usernameField: 'username',
passwordField: 'passwords',
passReqToCallback: true
}, async (req, username, passwords, done) => {
var user = {
username : req.body.username,
passwords :req.body.passwords
};
user.passwords = await helpers.encryptPassword(passwords);
// Saving in the Database
request.input('username',user.username);
request.input('passwords',user.passwords);
const result = await request.query("insert signUp values(@username,@passwords)");
user.id = result.insertId;
return done(null, user);
}));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(async (id, done)=> {
const rows = await pool.query(`SELECT * FROM signUp WHERE id =${id}`);
done(null, rows[0]);
});
routes.js
// SIGNUP
router.get('/signup', (req, res) => {
res.render('auth/signup');
});
router.post('/signup', passport.authenticate('local.signup', {
successRedirect: '/profile',
failureRedirect: '/signup',
failureFlash: true
}));
// SINGIN
router.get('/signin', (req, res) => {
res.render('auth/signin');
});
router.post('/signin', (req, res, next) => {
req.check('username', 'Username is Required').notEmpty();
req.check('passwords', 'Password is Required').notEmpty();
const errors = req.validationErrors();
if (errors.length > 0) {
req.flash('message', errors[0].msg);
res.redirect('/signin');
}
passport.authenticate('local.signin', {
successRedirect: '/profile',
failureRedirect: '/signin',
failureFlash: true
})(req, res, next);
});
router.get('/logout', (req, res) => {
req.logOut();
res.redirect('/');
});
router.get('/profile', isLoggedIn, (req, res) => {
res.render('profile');
});
module.exports = router;