[英]Passport-facebook unable to connect
In my application, I'm trying to add loggin with facebook account. 在我的应用程序中,我尝试使用Facebook帐户添加登录名。 As I'm using passport for the moment with local strategy, I tried to add the facebook strategy.
由于我暂时将护照与本地策略结合使用,因此我尝试添加Facebook策略。 I registered on facebook developper site to have my token and secret.
我在facebook开发者网站上注册以获得我的令牌和秘密。 I simply copy/paste source code portions from the official passport github, adapting to my personal use.
我只是从官方护照github复制/粘贴源代码部分,以适应我的个人用途。 The problem occurs when calling
呼叫时发生问题
passport.authenticate('facebook');
I am stuck in here and not redirected to the facebook loggin page. 我被困在这里,没有重定向到Facebook登录页面。 My application is waiting for a reponse or waits to be redirected, but nothing happens.
我的应用程序正在等待响应或等待被重定向,但是什么也没有发生。 I tried to provide on the facebook developper page my callback URL, and I tried without it (passing the callback throw the passport strategy).
我试图在facebook开发人员页面上提供我的回调URL,但我尝试不提供它(通过回调传递护照策略)。
What am I missing ? 我想念什么?
My application: 我的应用程序:
app.get('/auth/facebook', user_routes.facebookLogin);
app.get('/auth/facebook/callback', user_routes.facebookCallback);
My routes: 我的路线:
exports.facebookLogin = function(req, res, next) {
console.log('Try to login with facebook');
passport.authenticate('facebook'); //<---------------- does not go further than here
};
exports.facebookCallback = function(req, res, next){
passport.authenticate('facebook', {
successRedirect: '/home',
failureRedirect: '/login' });
};
My strategy: 我的策略:
passport.use(new FacebookStrategy({
clientID: "xxxxxxxx",
clientSecret: "xxxxxxxxxxx",
callbackURL: "http://localhost:8080/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOne({ username: profile.displayName, email: profile.emails[0].value }, function(err, olduser) {
if (err) { return done(err); }
if (olduser) {
return done(null, olduser);
}
else{
var newuser = new User({
username: profile.displayName,
email: profile.emails[0].value
}).save(function(err,newuser){
if(err) console.log(err);
done(null,newuser);
});
}
});
})
);
[EDIT] [编辑]
Changing the routing for the callback to this solves my issue. 将回调路由更改为此可以解决我的问题。 However, I dont understand the reason why...
但是,我不明白为什么...
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/home');
});
Finally found the solution here: 终于在这里找到了解决方案:
Using PassportJS with Connect for NodeJS to authenticate Facebook Users 将PassportJS与Connect for NodeJS结合使用以对Facebook用户进行身份验证
and here: 和这里:
Facebook Authentication with Passport and ExpressJS - Why is verify callback not called? 使用Passport和ExpressJS的Facebook身份验证-为什么未调用验证回调?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.