最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

[尝试登录我的应用程序时,我一直收到401错误

IT培训 admin 10浏览 0评论

[尝试登录我的应用程序时,我一直收到401错误

我设置了一个用户/登录路线,该路线可提取用户,将用户密码与哈希密码进行比较,生成令牌,然后将令牌发送到前端,但是由于某些原因,我在以下位置始终遇到401错误前端的控制台,最后进入我的最后一个捕获块。

我已经尝试将控制台注销,它进入“ hello”状态,但没有进入令人困惑的第一个.then()。此外,它有时似乎可以在本地运行,但在我的heroku服务器上却永远无法运行。

app.post("/user/login", (req, res, next) => {
  console.log('hello')
  let fetchedUser;
  User.find({ email: req.body.email }).then(user => {
      if (!user) {
        return res.status(401).json({
          message: "Auth failed1"
        });
      }
      fetchedUser = user[0];
      console.log('req.body.password', req.body.password)
      console.log('user password', user.password)
      return bcryptpare(req.body.password, user[0].password);
    }).then(result => {
      if (!result) {
        return res.status(401).json({
          message: "Auth failed2"
        });
      }
      const token = jwt.sign(
        { email: fetchedUser.email, userId: fetchedUser._id },
        process.env.JWT_SECRET,
        { expiresIn: "1h" }
      );
      console.log(token);
      res.status(200).json({
        token: token,
        expiresIn: 3600
      });
    })
    .catch(err => {
      return res.status(401).json({
        message: "AUTH FAILED 3"
      });
    });
});

我希望用户在使用此路径后才能登录,但是相反,它在最后一个catch块中引发了错误。它甚至都不会进入我的heroku服务器上的console.log('hello')。

回答如下:

要弄清楚实际上导致它到达.catch()的原因,您需要找到一种方法来记录err获得的实际.catch()。通常,这将告诉您是什么原因导致了承诺被拒绝,或者至少告诉您了在哪里进一步寻找。

如果在服务器上登录时遇到麻烦,也可以在发送回客户端的响应消息中添加更多信息,并在那儿获取信息。

仅供参考,您还有另一个问题。当您在return res.status(401).json({ message: "Auth failed1"});中执行.then()时,会将解释器发送到下一个.then()处理程序,这不是您想要的。

您可以通过执行throw someError并将所有401响应发送到.catch()来简化代码,在该位置您可以将401响应发送到一个位置。这将导致它跳过您想要的其他.then()处理程序。

[尝试登录我的应用程序时,我一直收到401错误

我设置了一个用户/登录路线,该路线可提取用户,将用户密码与哈希密码进行比较,生成令牌,然后将令牌发送到前端,但是由于某些原因,我在以下位置始终遇到401错误前端的控制台,最后进入我的最后一个捕获块。

我已经尝试将控制台注销,它进入“ hello”状态,但没有进入令人困惑的第一个.then()。此外,它有时似乎可以在本地运行,但在我的heroku服务器上却永远无法运行。

app.post("/user/login", (req, res, next) => {
  console.log('hello')
  let fetchedUser;
  User.find({ email: req.body.email }).then(user => {
      if (!user) {
        return res.status(401).json({
          message: "Auth failed1"
        });
      }
      fetchedUser = user[0];
      console.log('req.body.password', req.body.password)
      console.log('user password', user.password)
      return bcryptpare(req.body.password, user[0].password);
    }).then(result => {
      if (!result) {
        return res.status(401).json({
          message: "Auth failed2"
        });
      }
      const token = jwt.sign(
        { email: fetchedUser.email, userId: fetchedUser._id },
        process.env.JWT_SECRET,
        { expiresIn: "1h" }
      );
      console.log(token);
      res.status(200).json({
        token: token,
        expiresIn: 3600
      });
    })
    .catch(err => {
      return res.status(401).json({
        message: "AUTH FAILED 3"
      });
    });
});

我希望用户在使用此路径后才能登录,但是相反,它在最后一个catch块中引发了错误。它甚至都不会进入我的heroku服务器上的console.log('hello')。

回答如下:

要弄清楚实际上导致它到达.catch()的原因,您需要找到一种方法来记录err获得的实际.catch()。通常,这将告诉您是什么原因导致了承诺被拒绝,或者至少告诉您了在哪里进一步寻找。

如果在服务器上登录时遇到麻烦,也可以在发送回客户端的响应消息中添加更多信息,并在那儿获取信息。

仅供参考,您还有另一个问题。当您在return res.status(401).json({ message: "Auth failed1"});中执行.then()时,会将解释器发送到下一个.then()处理程序,这不是您想要的。

您可以通过执行throw someError并将所有401响应发送到.catch()来简化代码,在该位置您可以将401响应发送到一个位置。这将导致它跳过您想要的其他.then()处理程序。

发布评论

评论列表 (0)

  1. 暂无评论