如果剩下的200个帖子等待,这意味着什么?

发布于 2025-02-08 08:04:36 字数 1834 浏览 0 评论 0原文

我正在帖子登上以身份验证用户。内接口为“/用户/登录”。我发表帖子并接收状态代码200,但是邮递员以及我的客户正在等待未到达的RES对象。

这是通过Postman的API呼叫的屏幕截图:

这是服务器端的控制器:

router.post(
    '/user/login',
    passport.authenticate('local'),
    UserCtrl.getLogin,
)

getLogin =  (req, res, next) => {
        console.log("req: ", req.body)
        console.log('logged in', req.user);
        var userInfo = {
            username: req.user.username
        };
        res.json(userInfo)   
    }

控制台在控制器中打印行,并且用户有效地验证,例如:

req:  { username: 'fedex', password: 'fedex' }
logged in {
  _id: new ObjectId("62a8b00f468c563699d7dfc2"),
  username: 'fedex',
  password: '$2a$10$cdbh0oCBNpHxxwebsvArLOAFwetVAh5LTnQwk1Lg9kjWkjAWhfxym',
  __v: 0
}

问题可能在于本地护照策略的调用,但我只有执行标准:

const strategy = new LocalStrategy(

    function(username, password, done) {

        User.findOne({ username: username }, (err, user) => {
            if (err) {
                return done(err)
            }
            if (!user) {
                return done(null, false, { message: 'Incorrect username' })
            }
            if (!user.checkPassword(password)) {
                return done(null, false, { message: 'Incorrect password' })
            }

            return done(null, user)
        })
    }
) 

编辑:如果我删除了调用Passport.authenticate('local')的中间件,并将身份验证功能直接合并到控制器中,则可以工作。但是,在路线上呼叫护照有什么问题?

之前(不起作用):(

router.post(
    '/user/login',
     passport.authenticate('local'),
    UserCtrl.getLogin
)

在控制器内添加护照身份验证,它可以工作)

router.post(
    '/user/login',     
    UserCtrl.getLogin 
)

I am doing a POST to an enpoint to authenticate users. The endopoint is "/user/login". I make a post and receive status code 200 but the Postman, and also my client, are waiting for the RES object that does not arrive.

This is a screenshot of the API call through Postman:
enter image description here

This is controler in server side:

router.post(
    '/user/login',
    passport.authenticate('local'),
    UserCtrl.getLogin,
)

getLogin =  (req, res, next) => {
        console.log("req: ", req.body)
        console.log('logged in', req.user);
        var userInfo = {
            username: req.user.username
        };
        res.json(userInfo)   
    }

The console prints the lines in the controller, and the user is effectively authenticated, for example:

req:  { username: 'fedex', password: 'fedex' }
logged in {
  _id: new ObjectId("62a8b00f468c563699d7dfc2"),
  username: 'fedex',
  password: '$2a$10$cdbh0oCBNpHxxwebsvArLOAFwetVAh5LTnQwk1Lg9kjWkjAWhfxym',
  __v: 0
}

probably the problem is in the invocation of the local passport strategy, but I only do the standard:

const strategy = new LocalStrategy(

    function(username, password, done) {

        User.findOne({ username: username }, (err, user) => {
            if (err) {
                return done(err)
            }
            if (!user) {
                return done(null, false, { message: 'Incorrect username' })
            }
            if (!user.checkPassword(password)) {
                return done(null, false, { message: 'Incorrect password' })
            }

            return done(null, user)
        })
    }
) 

EDITED: If I remove the middleware where the passport.authenticate('local') is invoked, and incorporate the authentication functionality directly in the controller, it works. But what is wrong with calling passport in the route?

BEFORE (does not work):

router.post(
    '/user/login',
     passport.authenticate('local'),
    UserCtrl.getLogin
)

AFTER (adding passport authentication inside the controller, it work)

router.post(
    '/user/login',     
    UserCtrl.getLogin 
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文