Mas*_*iar 2 registration node.js passport.js
首先,我对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);
})
);
Run Code Online (Sandbox Code Playgroud)
这就是我如何应对POST上/register:
/* Handle Registration POST */
app.post('/register', passport.authenticate('signup', {
successRedirect: '/',
failureRedirect: '/failure_registration',
failureFlash : true
}));
Run Code Online (Sandbox Code Playgroud)
这总是带给我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);
});
});
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,failRedirect总是被执行
首先要诊断,我使用了自定义回调方法 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);
});
Run Code Online (Sandbox Code Playgroud)
然后我可以看到隐藏的错误是什么
authenticate
null
false
{ message: 'Missing credentials' }
Run Code Online (Sandbox Code Playgroud)
然后我很容易诊断,我在请求中发送JSON而不是FORM字段
通过改变来修复
app.use(bodyParser.urlencoded({ extended: true }));
Run Code Online (Sandbox Code Playgroud)
至
app.use(bodyParser.json());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3951 次 |
| 最近记录: |