【问题标题】:Ajax call to passport does not run the authenticate function对护照的 Ajax 调用不运行身份验证功能
【发布时间】:2017-10-24 12:03:05
【问题描述】:

我不想在我的注册路线中重定向,这是一个两阶段的过程(我目前想出的方式),所以我想提交第一个表单,使用 ajax 保存到 db 并返回一些东西,然后显示第二个表格来完成注册。发布路线有效,但功能不运行

router.route('/register')
  .post((req, res, next) => {
      console.log('this bit here works');
    passport.authenticate('local-signup', function(error, user) {
      console.log('it's here that nothing happens');
    if(error) {
      return res.status(500).json(error);
    }
      return res.json(user); //this is what I want to return;
  })
})

护照是否只适用于一次邮寄电话?

【问题讨论】:

  • 语法错误console.log('it\'s here that nothing happens');' 之前通知\

标签: node.js ajax passport.js


【解决方案1】:

passport.authenticate() 是 Express 中间件,不是常规函数。

按照the fine manual(搜索“自定义回调”),应该这样使用:

router.route('/register').post((req, res, next) => {
  console.log('this bit here works');
  passport.authenticate('local', function(error, user, info) {
    console.log("it's here that something should happen now.");
    if (error) {
      return res.status(500).json(error);
    }
    return res.json(user);
  })(req, res, next);
})

FWIW,user 不一定是正确的对象(例如,如果身份验证失败)。

【讨论】:

    猜你喜欢
    • 2017-12-10
    • 2022-10-06
    • 2018-11-06
    • 2015-09-06
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2016-10-02
    • 2019-03-10
    相关资源
    最近更新 更多