我想以不同的方式处理以下与passport-facebook相关的操作。
注册'我想做/检查:
登录'并且'连接帐户'我想进行一系列其他检查/操作。
我查看了passport documentation for facebook和passport-facebook module以及一堆相关的Stack Overflow问题,但我仍然在努力解决这个问题。
如何使用不同的callbackURL和选项实现不同的facebook策略(通过护照)?
答案 0 :(得分:8)
经过大量的挖掘,这就是我最终要做的事情:
制定3种不同的护照策略。
每个都有不同的名称(facebookSignUp vs facebookLogIn vs facebookConnect)和不同的callbackURL路径(... / sign-up / clbk vs ... / log-in / clbk vs ... / connect / clbk)。
// facebook strategy 1 (sign-up with facebook)
passport.use('facebookSignUp', new FacebookStrategy({
clientID: FACEBOOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: 'http://website/auth/facebook/sign-up/clbk'
},
function(accessToken, refreshToken, profile, clbk) {
return clbk(profile);
}
));
// facebook strategy 2 (log-in with facebook)
...
// facebook strategy 3 (connect facebook to existing account)
...
为每个策略创建路线。
初始请求路线& facebook身份验证后的回调路由。每个策略有2条路线。
var member = require('../member'); // member module controller
// sign-up with facebook
app.route('/auth/facebook/sign-up')
.get(member.facebookSignUp); // passport redirect to facebook for authentication
// sign-up with facebook callback
app.route('/auth/facebook/sign-up/clbk')
.get(
member.facebookSignUpClbk, // parse facebook profile to create new user & check for existing account -> redirect to log-in route
member.checkEmail, // check if email is already used -> throw error 'please log in with email, then connect facebook in settings'
member.checkUrl, // get unique url
member.signUp, // create new user & sign-in
member.email.welcome, // send welcome email
member.facebookRedirectDashboard // redirect to member dashboard
);
// log-in with facebook
app.route('/auth/facebook/log-in')
.get(member.facebookLogIn); // passport redirect to facebook for authentication
// log-in with facebook callback
app.route('/auth/facebook/log-in/clbk')
.get(
member.facebookLogInClbk, // authenticate user and log-in & check if user exists (fb oauth id or email address) -> throw error 'please sign up with facebook or log in with email'
member.lastLogin, // update user's last login field
member.facebookRedirectDashboard // redirect to dashboard
);
// connect facebook profile to existing account
...
// connect facebook profile to existing account clbk
...
在以下文件/功能中执行实际的护照身份验证。
member.facebookSignUp只调用passport.authenticate(),重定向到facebook。
// member.facebookSignUp
exports.facebookSignUp = function(req, res, next) {
passport.authenticate('facebookSignUp', { // use the 'facebookSignUp' strategy
display: null, // null = let facebook decide (or 'page' (default), 'popup', 'touch', etc)
scope: [
'public_profile', // profile returned by default, but specified here anywhere
'email', // ask for email address
'user_location' // ask for location
]
})(req, res);
};
在facebook授权用户并重定向到回调路由后,会执行member.facebookSignUpClbk。这是其他一切发生的地方。
// member.facebookSignUpClbk
exports.facebookSignUpClbk = function(req, res, next) {
// parse profile & plug into req.body for new user creation in later fxn
function parseProfile(profile) {
// user doc
req.body = {};
// facebook profile data
req.body.facebook = (profile._json) ? profile._json : {id: profile.id};
// name
...
// email
...
next();
}
// check existing users (mongoose/mongodb)
function checkUser(profile) {
User.findOne({query}, function(err, userDoc) {
if (err) {
return res.redirect(
'http://'+req.headers.host+
'?header=Sign-Up Error!'+
'&message=We had trouble signing you up with Facebook. Please try again'
);
} else if (userDoc) {
// redirect to log-in fxn
return res.redirect('http://website/auth/facebook/log-in');
} else {
parseProfile(profile);
}
});
}
// passport authentication
function passportAuth() {
passport.authenticate('facebookSignUp', function(profile) {
if (!profile || !profile.id) {
return res.redirect(
'http://'+req.headers.host+
'?header=Sign-Up Error!'+
'&message=We had trouble signing you up with Facebook. Please try again or sign-up via email.'
);
} else {
checkUser(profile);
}
})(req, res);
}
// start process
passportAuth();
};
member.facebookLogIn,.facebookLogInClbk,.facebookConnect,& .facebookConnectClbk以类似的方式设置,只是在' Clbk'功能取决于我尝试做什么。
希望这有帮助!