首先,我对Passport.js很新,所以这可能最终成为一个非常天真的问题。我有这个作为注册的策略:
// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook');
app.use(expressSession({secret: 'mySecretKey'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
//[...]
passport.use('signup', new LocalStrategy({
name : 'name',
password : 'password',
email : 'email',
passReqToCallback : true
},
function(req, username, password, done) {
findOrCreateUser = function(){
// find a user in Mongo with provided username
User.findOne({'name':username},function(err, user) {
// In case of any error return
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists');
return done(null, false,
req.flash('message','User Already Exists'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.name = name;
newUser.password = createHash(password);
newUser.email = req.param('email');
/*newUser.firstName = req.param('firstName');
newUser.lastName = req.param('lastName');*/
// save the user
newUser.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
// Delay the execution of findOrCreateUser and execute
// the method in the next tick of the event loop
process.nextTick(findOrCreateUser);
})
);
这就是我在POST
上处理/register
的方式:
/* Handle Registration POST */
app.post('/register', passport.authenticate('signup', {
successRedirect: '/',
failureRedirect: '/failure_registration',
failureFlash : true
}));
此总是将我带到failureRedirect
链接,而不是成功。输入数据是正确的,我总是使用不同的用户和邮件进行注册。如果这是一个愚蠢的问题,我很抱歉,但我真的不明白为什么它永远不会进入successRedirect
。
感谢。
编辑:添加@robertklep的建议和更正,仍然没有工作。我想指出没有触发错误,也没有打印任何日志。EDIT2:序列化/反序列化功能:
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
答案 0 :(得分:8)
I had this same problem, failureRedirect was always being executed
First to diagnose, I used the custom callback method http://passportjs.org/docs/authenticate
app.post('/sign_in', function(req, res, next) {
console.log(req.url);
passport.authenticate('local-login', function(err, user, info) {
console.log("authenticate");
console.log(err);
console.log(user);
console.log(info);
})(req, res, next);
});
Then I could see what the hidden error was
authenticate
null
false
{ message: 'Missing credentials' }
Which then became easy for me to diagnoise, I was sending JSON in the request rather than FORM fields
Fixed by changing
app.use(bodyParser.urlencoded({ extended: true }));
To
app.use(bodyParser.json());
答案 1 :(得分:0)
你使用http或https吗?我有同样的情况。我修好了这个
app.use(expressSession({....
cookie: {
httpOnly: true,
secure: false // for http and true for https
}
}));
在我看来,护照无法收到cookies。
答案 2 :(得分:0)
我知道这已经快4岁了,但是如果有人遇到相同的问题,我使用了djeeg的诊断
app.post('/sign_in', function(req, res, next) {
console.log(req.url);
passport.authenticate('local-login', function(err, user, info) {
console.log("authenticate");
console.log(err);
console.log(user);
console.log(info);
})(req, res, next);
});
我得到: 空值 假 “缺少凭据”
解决方案:事实证明,我没有在HTML表单中使用“名称”,这意味着未读取数据,这就是错误的出处
<input name="email" type="email">
那为我解决了
答案 3 :(得分:0)
尝试检查你的表单的方法,它必须是method = post,以防我错误地把它写成GET方法,我花了3天时间才找到这个,因为没有错误