身份验证后页面显示为“空”

时间:2017-04-09 10:52:53

标签: node.js express passport.js

我正在创建一个简单的PWA,将多个数据源绘制到一个应用程序中。我目前正在尝试使用护照和推特策略的组合来设置身份验证。

用户成功通过身份验证后,Twitter会重定向到回调端点,并使用有效的用户数据....实际上,auth已成功完成。但是,当将用户发送回客户端应用程序时,会显示一个带有单词null的html文档,而不是应用程序。

使用以下代码,我希望:

  • Twitter返回回调网址
  • 服务器以authSuccess函数
  • 执行操作
  • 重定向到客户端上提供的网址
  • routes.js通过catch all route
  • 为应用程序shell提供服务
  • 用于启动和处理所提供的URL的客户端应用程序

目前,只有前两个步骤成功,应用程序只显示null(地址栏中的URL正确),而不是预期的输出。将writeHead()调用的位置更改为/有效,用户将按预期进行身份验证()。

routes.js

let users = require('../controllers/userController');

app.get('/user/auth/twitter/callback',
  passport.authenticate('twitter', {
    failWithError: true
  }),
  function(req, res) {
    console.log('[Twitter] Auth success route hit');
    users.authSuccess(req, res);
  },
  function(err, req, res, next) {
    console.log('[Twitter] Auth failure route hit');
    users.authFailure(err, req, res, next);
  }
);

app.get('*', function(req, res){
   console.log('[Route] Catch All: ' + req.path);
   res.sendFile(path.resolve(__dirname, '../../public/index.html'));
});

userController.js

authSuccess(req, res) {
  console.log('[User Controller] User',req.user);

  // Set some cookies here

  res.writeHead(302, {'Location': '/user/profile'});
  // res.redirect('/user/profile');
  res.end();
}

任何帮助非常感谢。如果您需要更多代码,请询问:)

由于

0 个答案:

没有答案
相关问题