模仿护照验证行为

时间:2018-08-20 08:01:00

标签: node.js express passport.js handler

我正在对不同的API使用passport.js身份验证,并且可以正常工作,但是我想为每个API分离代码,因此我一直无法调用passport.authenticate('facebook')。我将以Facebook代码为例。

这有效:

const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const fb = require('fb');

passport.use(new FacebookStrategy({
  clientID: 'XXXXXXXXXXXXXXXX',
  clientSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  callbackURL: "https://127.0.0.1/facebook/authCallback/"
}, function(accessToken, refreshToken, profile, cb) {
  return {accessToken, refreshToken, profile, cb};
}));

// Entry point
app.get('/authorize/:clientId/:network', passport.authenticate('facebook'));

app.get('/:network/authCallback', (request, response) => {
  // Some code here...
});

但是,当我尝试将其拆分为其他文件时,它从未从passport.authenticate('facebook')进行回调

sserve.js

const authUtils = require('./utils/authUtils');

app.get('/authorize/:clientId/:network', (request, response) => {
  authUtils.authorize(request.params.clientId, request.params.network);
});

authUtils.js

async function authorize(clientId, network) {    
  try {
    config = await databaseUtils.getConfig(clientId, network);

    if(typeof(config) !== 'undefined') {
      passport.use(new FacebookStrategy({
        clientID: config.appId,
        clientSecret: config.appSecretKey,
        callbackURL: 'https://127.0.0.1/' + network + '/authCallback/'
      }, function(accessToken, refreshToken, profile, cb) {
        return {accessToken, refreshToken, profile, cb};
      }));

      // It reachs to this point but never comeback...
      passport.authenticate(network);
    } else {
      throw new Error(network + ' is not configured');
    }
  } catch (e) {
    throw e;
  }
}

更新

经过一些调试测试后,问题似乎出在passport.authenticate(network)没有重定向到回调URL。

如果我添加:

app.get('/authorize/:clientId/:network', (request, response) => {
  authUtils.authorize(request.params.clientId, request.params.network);
  console.log(1);
  next();
});

控制台显示“ 1”,浏览器返回404 Not Found。 Passport不会继续进行重定向过程。

1 个答案:

答案 0 :(得分:0)

表达方式知道在中间件的这一步中一切都很好,就是调用“ next”方法,这是第三个参数(请求,响应,下一个)。 因此,尝试将第三个参数传递给authorize方法,并在进行护照认证后调用它:

sserve.js:

const authUtils = require('./utils/authUtils');

app.get('/authorize/:clientId/:network', (request, response, next) => {
  authUtils.authorize(request.params.clientId, request.params.network, response);
});

authUtils.js:

async function authorize(clientId, network, response) {    
  try {
    config = await databaseUtils.getConfig(clientId, network);

    if(typeof(config) !== 'undefined') {
      passport.use(new FacebookStrategy({
        clientID: config.appId,
        clientSecret: config.appSecretKey,
        callbackURL: 'https://127.0.0.1/' + network + '/authCallback/'
      }, function(accessToken, refreshToken, profile, cb) {
        return {accessToken, refreshToken, profile, cb};
      }));

      // It reachs to this point but never comeback...
      passport.authenticate(network);
      return response.send();
    } else {
      throw new Error(network + ' is not configured');
    }
  } catch (e) {
    throw e;
  }
}