node.js – 使用Passport和ExpressJS进行Facebook身份验证 – 为什么不调用验证回调?

前端之家收集整理的这篇文章主要介绍了node.js – 使用Passport和ExpressJS进行Facebook身份验证 – 为什么不调用验证回调?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Passport,用于在ExpressJS应用程序中验证用户身份.我试图将所有Facebook路由放在他们自己的模块中,因为我打算支持其他OAuth提供商.在我的启动脚本中,我已经定义了所需的FB端点:
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var facebook = require('./routes/facebook');
var login = require('./routes/login');
var http = require('http');
var path = require('path');

var app = express();

/* Configuration stuff here*/

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/login',login.index);
app.get('/auth/facebook',facebook.fb_auth);
app.get('/auth/facebook/callback',facebook.fb_callback);

如你所见,我需要我的’facebook’模块,其中包含实际路线和Facebook验证回调.它看起来像这样:

var passport = require('passport'),FacebookStrategy = require('passport-facebook').Strategy,User = require('../services/user');

passport.use(new FacebookStrategy({
  clientID: 'myclientid',clientSecret: 'mysecretkey',callbackURL: "http://localhost:3000/auth/facebook/callback" //localhost:3000 domain is registered domain in fb
},function(accessToken,refreshToken,profile,done) {
   console.log('verify')
  User.findOrCreate(profile,function(err,user){
      if(err) return done(err);
      done(null,user);
  });
}
));

// Redirect the user to Facebook for authentication.  When complete,// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
exports.fb_auth = function(req,res){
  passport.authenticate('facebook')
};
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted,the user will be logged in.  Otherwise,// authentication has Failed.
exports.fb_callback = function(req,res){
  console.log('callback')
  passport.authenticate('facebook',{ successRedirect: '/',failureRedirect: '/login' });
};

我可以看到(记录到stdout)调用了fb_auth但是上面定义的验证回调函数永远不会.我忽略了什么吗?在我可以捕获的某个地方是否发生错误

谢谢!

解决方法

我在这里找到了答案: Using PassportJS with Connect for NodeJS to authenticate Facebook Users

您需要显式调用’authenticate’函数并为其提供req,res和next.

exports.fb_auth = function(req,res,next){
  passport.authenticate('facebook')(req,next);
  return;
};

exports.fb_callback = function(req,next){
  passport.authenticate('facebook',failureRedirect: '/login' })(req,next);
};
原文链接:https://www.f2er.com/nodejs/241156.html

猜你在找的Node.js相关文章