在 Heroku 上托管后重定向你太多次错误

Redirected you too many times error after hosting on Heroku

我刚刚在 Heroku 上托管了我的 Node.js 应用程序。我在前端使用 Handlebars 模板。登录后,我应该被重定向到我的仪表板,但我收到了以下错误,

fyngram-dev.herokuapp.com redirected you too many times.

一切都在我的本地服务器上运行,但在生产中没有经过用户登录。

我已经尝试了我在网上找到的解决方案,并且 none 目前为止有效。

但我的主要问题是如何确切知道重定向错误的原因,以便我可以确切地知道应用什么解决方案?

这些是目前感兴趣的路线

获取登录页面

let authorization;

router.get("/admin/signin", async (req, res) => {
  if (!authorization) {
    return res.render("signin", { title: "Admin Signin" });
  } else {
    return res.redirect("/dashboard");
  }
});

登录

router.post("/admin/signin", async (req, res) => {
  try {
    // check if user exists with username
    const checkUsername = await User.findOne({
      username: req.body.key,
      userType: "admin",
    });

    if (!checkUsername) {
      // if username doesn't exist, check if email exists
      const checkEmail = await User.findOne({
        email: req.body.key,
        userType: "admin",
      });

      if (!checkEmail) {
        // if email doesn;t exist, return error message

        return res.render("signin", {
          message: "User doesn't exist.",
          messageClass: "alert-danger",
          title: "Admin Signin",
        });

      } else {
        // if email exists, check if password is correct
        const isMatch = await bcrypt.compare(
          req.body.password,
          checkEmail.password
        );

        if (!isMatch) {
          return res.render("signin", {
            message: "Incorrect email, username or password",
            messageClass: "alert-danger",
            title: "Admin Signin",
          });

        } else {
          // if login with email works, generate auth token and return user
          const token = await checkEmail.generateAuthToken();

          req.headers.authorization = `Bearer ${token}`;

          authorization = req.headers.authorization;

          return res.redirect("/dashboard");
        }
      }
    } else {
      // if username exists, check if password is correct
      const isMatch = await bcrypt.compare(
        req.body.password,
        checkUsername.password
      );

      if (!isMatch) {
        return res.render("signin", {
          message: "Incorrect email, username or password",
          messageClass: "alert-danger",
          title: "Admin Signin",
        });
      } else {
        // if password is correct, generate auth token and return user
        const token = await checkUsername.generateAuthToken();

        req.headers.authorization = `Bearer ${token}`;

        authorization = req.headers.authorization;

        return res.redirect("/dashboard");
      }
    }
  } catch (e) {
    return res.render("signin", {
      message: "Invalid Credentials",
      messageClass: "alert-danger",
      title: "Admin Signin",
    });
  }
});

获取仪表板页面

router.get("/dashboard", async (req, res) => {
    try {
      const token = authorization.replace("Bearer ", "");

      const decoded = jwt.verify(token, process.env.JWT_SECRET);

      const loggedInUser = await User.findOne({
        _id: decoded._id,
        "tokens.token": token,
        userType: "admin",
      }).lean();

      if (!loggedInUser) {
        return res.redirect("/admin/signin");
      } else {
        ...
        return res.render("dashboard", {
          loggedInUser,
          title: "Dashboard",
        });
      }
    } catch (e) {
      return res.redirect("/admin/signin");
    }
});

任何帮助我指明正确方向的人都将不胜感激

仔细调试我的应用程序后,我发现问题不是出在我的 Heroku 部署上。相反,它来自我的应用程序逻辑。我正在检查可能未定义的动态值。 (我的错)。我添加了一个默认值以防止再次出现错误。