[英]Passport JS authentication issue - Running on NodeJS - Getting error 503 - H13 on Heroku
我已經搜索並閱讀了許多類似的情況,但沒有一個與我的非常相似。 我已經使用 PostgreSQL 數據庫創建了一個在 NodeJS 上運行的 API。 我已經使用護照與本地護照進行身份驗證。
這是我聲明 Passport 配置的地方:
const express = require("express");
const auth = express.Router();
const bcrypt = require("bcrypt");
const pool = require("../../config/dbConfig");
require("dotenv").config();
const session = require("express-session");
const flash = require("express-flash");
const passport = require("passport");
const initializePassport = require("../../config/passportConfig");
initializePassport(passport);
auth.use(express.urlencoded({ extended: false }));
auth.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
auth.use(passport.initialize());
auth.use(passport.session());
auth.use(flash());
const checkAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return res.redirect("/api/auth/dashboard");
}
next();
};
const checkNotAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
next();
}
res.redirect("/api/auth/login");
};
auth.get("/", (req, res) => {
res.redirect("/api/auth/login");
});
auth.get("/login", checkAuthenticated, (req, res) => {
res.render("login");
});
auth.get("/signup", checkAuthenticated, (req, res) => {
res.render("signup");
});
auth.get("/dashboard", checkNotAuthenticated, (req, res) => {
res.render("dashboard", { user: req.user.first_name });
});
auth.post("/signup", async (req, res) => {
const { email, first_name, last_name, username, password, password2 } =
req.body;
let errors = [];
if (
!email ||
!first_name ||
!last_name ||
!username ||
!password ||
!password2
) {
errors.push({ message: "All fields need to be filled." });
}
if (password.length < 6) {
errors.push({ message: "Password needs to be 6 characters or more." });
}
if (password !== password2) {
errors.push({
message: "The two passwords do not match. Please try again.",
});
}
if (errors.length > 0) {
res.render("signup", { errors });
} else {
//Form validation passed
let hashedPassword = await bcrypt.hash(password, 10);
pool.query(
"SELECT * FROM users WHERE email = $1",
[email],
(err, results) => {
if (err) {
throw err;
}
if (results.rows.length > 0) {
errors.push({
message:
"The email has already been used. Please login or use a different email.",
});
res.render("signup", { errors });
} else {
pool.query(
"INSERT INTO users (email, password, first_name, last_name, username) VALUES ($1, $2, $3, $4, $5);",
[email, hashedPassword, first_name, last_name, username],
(err, results) => {
if (err) {
throw err;
}
req.flash(
"success_msg",
"You are now registered. Please login to continue."
);
res.redirect("/api/auth/login");
}
);
}
}
);
}
});
auth.post(
"/login",
passport.authenticate("local", {
successRedirect: "/api/auth/dashboard",
failureRedirect: "/api/auth/login",
failureFlash: true,
})
);
auth.get("/logout", (req, res) => {
req.logOut();
req.flash("success_msg", "You have been succesfuly logged out.");
res.redirect("/api/auth/login");
});
module.exports = auth;
一切都在本地運行順利,但是一旦我在 Heroku 上部署,我收到錯誤 503/H13:
heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/api/auth/dashboard" host=salty-ridge-11669.herokuapp.com request_id=5344a940-61ab-4f35-bb49-68a05b76ea12 fwd="94.4.235.211" dyno=web.1 connect=0ms service=8ms status=503 bytes=0 protocol=https
我嘗試使用自定義 function 並將 req.redirect 傳遞給 passport.authenticate (而不是 successRedirect)。 從我在網上收集的信息來看,https 存在某種問題? 但是請求只是重定向,所以我真的無法理解。
好的,謎團解開了。 這只是我創建的中間件中缺少的 else 語句:
const checkAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return res.redirect("/api/auth/dashboard");
} else { //HERE
next();
}
};
const checkNotAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
next();
}else{ //AND HERE
}
res.redirect("/api/auth/login");
};
出於某種原因,我調用 next() 的事實在我的大腦中已經足夠了。
現在本地和 Heroku 部署的版本都在工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.