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

标签 node.js oauth-2.0 passport.js passport-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' });
};

我可以看到(记录到标准输出)调用了 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, res, next);
  return;
};

exports.fb_callback = function(req, res, next){
  passport.authenticate('facebook', { successRedirect: '/',
    failureRedirect: '/login' })(req, res, next);
};

关于node.js - 使用 Passport 和 ExpressJS 进行 Facebook 身份验证 - 为什么不调用验证回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507941/

相关文章:

node.js - 为什么 Electron 和 Node.JS 之间的模块版本不匹配?

javascript - 使用 oauth2 检索 Google 地方信息 client_id

Salesforce 返回 "unsupported_grant_type"

javascript - 使用 Passportjs 刷新页面后保持身份验证

javascript - passport.js 成功认证不调用 next()

javascript - 使用 MongoDB 数据库为每个查询打开一个新连接是好习惯吗?

javascript - Node JS 重定向

google-app-engine - 具有 AppEngine、Webapp2 和 Cloud Endpoints Proto Datastore 的多个 Auth Provider

node.js - Stormpath Express Passport 保存自定义数据甚至默认数据

javascript - Express.js 4 路由与 router.route 不匹配