我剛剛在 Heroku 上托管了我的 Node.js 應用程式。我在前端使用 Handlebars 模板。登錄后,我應該被重定向到我的儀表板,但我收到了以下錯誤,
fyngram-dev.herokuapp.com 將您重定向了太多次。
一切都在我的本地服務器上運行,但在生產中不會超過用戶登錄。
我已經嘗試了我在網上找到的解決方案,但到目前為止都沒有奏效。
但我的主要問題是我如何確切知道重定向錯誤的原因是什么,以便我可以確切知道要應用什么解決方案?
這些是目前感興趣的路線
獲取登錄頁面
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");
}
});
任何幫助我指出正確的方向將不勝感激
uj5u.com熱心網友回復:
仔細除錯我的應用程式后,我發現問題不是來自我的 Heroku 部署。相反,它來自我的應用程式邏輯。我正在檢查一個可能未定義的動態值。(我的錯)。我添加了一個默認值以防止再次出現錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/421192.html
標籤:
上一篇:部署在Heroku上的應用程式不顯示來自MySqlDB的資料
下一篇:Python3.7與2.7,分配
