我正在尝试在我的个人网站中构建一个基本的身份验证系统,该系统由React提供支持。我希望能够使用data.users MongoDB Atlas集合中的文档定义的用户名和密码登录和注销。我按照this教程进行了个性化设置。一切都可以在localhost上完美运行,但是将项目部署到AWS Elastic Beanstalk时出现504网关超时。我正在部署3个docker容器:一个用于我的前端,一个用于我的nginx代理传递,另一个用于我的后端。我认为无论如何我的Nginx配置都不会出现问题,因为我的虚拟发布请求(与我所有身份验证内容的路由相同,并且由res.send(“ string”)组成)可以正常工作。只是我的前端发帖失败:
postLogin = (userName, password) => {
axios({
method: "post",
// url: "http://localhost:5000/auth/login",
url: "https://example.com/api/auth/login/",
data: {
name: userName,
password: password,
},
})
.then(
function (res) {
if (res.status === 200) {
this.setAuthTokens(res.data);
this.setIsError(false);
} else {
this.setIsError(true);
}
}.bind(this)
)
.catch((e) => {
this.setIsError(true);
});
};
我相关的文件结构是:
以下是相关文件:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
type: String,
required: true,
min: 1,
max: 20,
},
password: {
type: String,
required: true,
min: 1,
max: 2000,
},
});
module.exports = mongoose.model("User", userSchema);
const router = require("express").Router();
const User = require("../models/userModel");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const dotenv = require("dotenv").config({ path: "./.env" });
if (dotenv.error) {
throw dotenv.error;
}
//login
router.post("/login", async (req, res) => {
//check if user in db
try {
const user = await User.find({ "name": req.body.name });
if (user.length === 0) {
return res.status(400).send("Name not found.");
}
const bcryptRes = await bcrypt.compare(
req.body.password,
"my_generated_hashstring"
);
if (!bcryptRes) {
return res.status(400).send("Wrong password.");
}
const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET, {
expiresIn: "1h",
});
res.send({ user: user._id, token: token });
} catch (err) {
res.status(500).send(err);
}
});
// verify
router.post("/verify", (req, res) => {
let thingy = true;
jwt.verify(req.body.token, process.env.TOKEN_SECRET, function (err, decoded) {
if (err) {
console.log(err);
thingy = false;
}
});
res.send(thingy);
});
// dummy post
router.post("/test", (req, res) => {
res.send("uwa");
});
module.exports = router;
const mongoose = require("mongoose");
const express = require("express"),
app = express(),
PORT = 5000;
const bodyParser = require("body-parser");
const cors = require("cors");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//ROUTES
const authRoute = require("./routes/authRoute"); //contains register and login endpoints eg: /auth/register, /auth/login
app.use("/auth", authRoute);
//connect to mongodb
mongoose.connect(
process.env.DB_CONNECT,
{ useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false },
() =>
console.log("Connection to MongoDB established. I STILL FEEL ALIVEEEEEEE")
);
app.listen(PORT, () => console.log("Server listening on port " + PORT + "."));
这里有一个screenshot,用以证明我的data.users集合中有东西。
part of my eb log显示我确实连接到MongoDB。
请提前谢谢,让我知道是否需要提供更多详细信息。抱歉,如果我只是个疯子。
P.S。我认为这是猫鼬代码失败,而不是bcrypt.compare,因为我将print语句放在User.find()之前和之后,并且仅将User.find()之前的打印语句显示在我以前的eb日志中。我之所以发布此问题,而不是使用更多的打印语句进行调试,是因为进行eb部署会花费很长时间。我浏览了一些相关的StackOverflow链接,例如Mongoose .findOne() breaks when deployed和504 error when inserting into mongo database。但是,它们要么没有解决,要么是由于错别字。