【问题标题】:Passport.js custom error for unauthorized and bad request未经授权和错误请求的 Passport.js 自定义错误
【发布时间】:2019-08-05 23:32:18
【问题描述】:

实现passport-jwt 并尝试在用户为假时抛出自定义错误响应(电子邮件|密码为空`) 如果用户没有注册

api.js

const passport = require('passport');
const router = require('express-promise-router')();

const passportHelper = require('../helpers/passportHelper');
const authController = require('../controllers/authController');
const { badRequest } = require('../helpers/responseHelper');

const requireAuth = passport.authenticate('jwt', { session: false });
const requireSignin = passport.authenticate('local', { session: false }, (err, user, info) => {
    if(!user) {
        const err = new Error('please provide email and password.');
            err.status = 400;
            err.code = 'CP_SI_ValidationFailed';
            return err;
    }
});

router.post('/signup', authController.signup);
router.post('/signin', requireSignin, authController.signin);

module.exports = router;

当我尝试使用邮递员登录时,我没有得到任何响应

预期结果

{
    "error": {
        "code": "CP_SI_ValidationFailed",
        "message": "email and password is required"
    }
}

有没有其他方法可以处理 badRequest 并返回上面的 resonse ,

谢谢你

【问题讨论】:

    标签: node.js express error-handling jwt passport.js


    【解决方案1】:

    这是一个确实返回响应的更新答案。

    const requireSignin = (req, res, next) => {
      passport.authenticate('local', { session: false }, (err, user, info) => {
        if (err || !user) {
          const err = {};
          err.status = 400;
          err.code = 'CP_SI_ValidationFailed';
    
          return res.json(err); // send the error response to client
        }
        return next(); // continue to next middleware if no error.
    
      })(req, res, next); /* passport.authentication returns a function,
                             we invoke it with normal req..res arguments 
                             to override default functionality */ 
    }
    

    【讨论】:

      【解决方案2】:

      从 passport.authenticate 回调中返回错误是不够的。返回值不被护照处理。如果要向客户端返回自定义错误,则应编写自定义中间件。如果您将 requireSignin 方法更改为以下内容,它应该可以正常工作:

      const requireSignin = function(req, res, next) {
          passport.authenticate('local', { session: false }, (err, user, info) => {
              if(err || !user) {
                  const err = {};
                  err.status = 400;
                  err.code = 'CP_SI_ValidationFailed';
      
                  return res.json(err); // send the error response to client
              } 
              return next(); // continue to next middleware if no error.
          });
      });
      

      【讨论】:

      • 非常感谢您抽出宝贵的时间,尝试了您的解决方案,但完全没有得到任何回复。所以我在控制台中尝试了这个console.log('test 1'); passport.authenticate('local', { session: false }, (err, user, info) => { console.log('test 2'); if(err || !user) { console.log('test 3');,我只能看到test 1
      • 您已经设置了本地策略,不是吗?查看护照文件,了解如何设置本地策略:passportjs.org/docs/configure
      • 是的,我已经设置了passport-local 策略谢谢,我采取了另一种方法,创建了一个中间件来验证和检查用户是否注册。 router.post('/signin', validateUser, requireSignin, authController.signin); 请提供您对此的意见。
      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多