我正在尝试使用Nodejs创建一个表单登录名,我正在使用mssql
和passport.js
,但是它总是将我重定向到失败登录名,我检查了在数据库中创建的用户,但是这些还可以我已删除bcrypt
进行测试,但仍然存在相同的问题。
有人可以帮我吗?预先感谢。
这是我的passport.js
文件的一部分:
passport.serializeUser(function(usario, done) {
done(null, usuario.id);
});
// used to deserialize the user
passport.deserializeUser(function(Id, done) {
request.query("select * from tb_User where Id='"+Id+"'",function(err,rows){
done(err, rows[0]);
});
});
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'usuario',
passwordField : 'Password',
passReqToCallback : true // allows us to pass back the entire request to the callback
}),
function(req, usuario, Password, done) { // callback with email and password from our form
request.query("select * from tb_User where usuario='"+usuario+"'",function(err,rows){
if (err){
return done(err);
}
if (!rows.length) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // ash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!(rows[0].Password == Password)){
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, rows[0]);
console.log('loged');
}
});
};
router.js
module.exports = function(app,passport) {
app.get('/index', function(req, res) {
res.render('/index.html',{ message: req.flash('loginMessage') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/view/menu.html', // redirect to the secure profile section
failureRedirect : '/login.html', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}),
function(req, res) {
console.log("hello");
if (req.body.remember) {
req.session.cookie.maxAge = 1000 * 60 * 3;
} else {
req.session.cookie.expires = false;
}
res.redirect('/');
});
app.get('/logout', function(req, res) {
req.session.destroy();
req.logout();
res.redirect('/login.html');
});
function isLoggedIn(req,res,next){
// if user is authenticated in the session, carry on
if(req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/view/menu.html');
}
};