我正在使用compoundjs,compound-passport和passport-facebook开发一个简单的用户crud模块。它使用默认方法生成crud,如果我想将它保存到数据库,它可以正常工作但是我想将它保存到会话中并在稍后阶段保存用户唯一的问题是我没有得到请求或模型中的会话。
这是我的user.js模型
module.exports = function (compound, User) {
// define User here
User.findOrCreate = function (data, done) {
console.log(data.profile.emails[0].value);
/* FACEBOOK OPENID */
if (data.profile.provider == "facebook") {
User.all({
where: {
email_id: data.profile.emails[0].value
}, limit: 1
}, function (err, user) {
if(data.profile.gender != '' && data.profile.gender != undefined)
var givenGender = (data.profile.gender == 'male')?'M':'F'
else
var givenGender = '';
if (user[0]) return done(err, user[0]);
var userDet = {
firstname: data.profile.name.givenName,
lastname: data.profile.name.familyName,
email_id: data.profile.emails[0].value,
gender:givenGender,
dob: new Date(data.profile._json.birthday),
registered_from:"facebook",
created_at:new Date(),
updated_at:new Date()
};
req.session.userdet = userDet;//This does not work
done();
//req.session.userdet = userDet;
// User.create({
// displayName: data.profile.displayName,
// email: data.profile.emails[0].value,
// facebookID: data.openId,
// provider:"facebook",
// createDate:new Date(),
// lastLogin:new Date()
// }, done);
});
} else
/* SOMETHING NOT KNOWN YET */
{
console.log("DONT NOW HOW TO HANDLE THIS USER.")
console.log(data.profile);
}
};
};
req.session.userdet = userDet;上面不起作用,因为我在模型中没有请求对象。
这是我的facebook.js实现了facebook策略
var passport = require('passport');
exports.callback = function(token, tokenSecret, profile, done) {
exports.User.findOrCreate({
facebookId: profile.id,
profile: profile
}, function (err, user) {
return done(err, user);
});
};
exports.init = function (conf, app) {
var Strategy = require('passport-facebook').Strategy;
passport.use(new Strategy({
clientID: conf.facebook.apiKey,
clientSecret: conf.facebook.secret,
callbackURL: conf.baseURL + 'users/signupnew'
}, exports.callback));
passport.serializeUser(function(user, done) {
console.log("I am here");
console.log(user);
done(null, user);
});
passport.deserializeUser(function(id, done) {
exports.User.findById(id, function(err, user) {
done(err, user);
});
});
// app.get('/auth/facebook',
// passport.authenticate('facebook', { scope: [ 'email,user_likes,user_birthday' ] }));
// app.get('/auth/facebook/callback',
// passport.authenticate('facebook', {
// failureRedirect: conf.failureRedirect || '/'
// }), exports.redirectOnSuccess);
app.get('/users/signupnew',passport.authenticate('facebook', {
failureRedirect: conf.failureRedirect || '/'
}),function(req,res){
req.session.redirect = '/users/signup';
exports.redirectOnSuccess(req,res);
});
};
基本上我想实现多步注册,这样我就可以收集会话中的所有数据,然后注册用户。如果我对流程的理解有问题,我很抱歉,因为我是节点和复合的新用户。