英文:
nodejs android studio and passport js login 404 error
问题
我在尝试通过我的 Android 应用程序登录到我的 Node.js 本地服务器时遇到了问题。
每次尝试登录时,我收到 404 错误。我可以使用 Post 路由成功注册用户,并且可以在我的 mongodb 数据库中看到具有用户名和密码(经过哈希和加盐处理)的用户。
但是,当我使用正确的凭据(用户名和密码)登录时,我始终收到 404 错误。我认为这意味着我的登录路由不正确。
要登录我的移动应用程序,它会将用户名和密码发布到登录路由,并在运行成功和失败路由时不会发布相应的控制台日志,我会收到 404 错误。我已经检查了用户名和密码,它们是正确的。
app.post("/signup", function(req, res)
{
User.register(new User({username: req.body.username, email: req.body.email}), req.body.password, function(error, user)
{
if(error)
{
console.log(error);
res.status(400).send();
}
else
{
passport.authenticate("local")(req, res, function(){//使用本地策略登录用户
res.status(200).send();
});
}
});
});
app.post("/login", passport.authenticate("local", {//中间件
successRedirect: "loginsuccess",
failureRedirect: "/loginfail"
}), function(req, res){
});
app.get("/loginsuccess",isLoggedIn, function(req, res)
{
res.status(200).send();
console.log("登录成功");
});
app.get("/loginfail", function(req, res)
{
console.log("登录失败");
res.status(400).send();
});
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
英文:
I have an issue when trying to login to my node js local server via my android application.
Each time I try to log in I receive 404 error. I can register users fine using the Post route and can see it in my mongodb database with the username and password(hashed+salted).
But when I log in with my credentials(username and password) correctly I always get 404 error. Which I think means my login route is not correct.
To login my mobile app posts to the login route and sends the username and password.
But on running my success and fail routes do not post their corresponding console logs and I get 404 error. I have checked the username and password and they are correct.
app.post("/signup", function(req, res)
{
User.register(new User({username: req.body.username, email: req.body.email}), req.body.password, function(error, user)
{
if(error)
{
console.log(error);
res.status(400).send();
}
else
{
passport.authenticate("local")(req, res, function(){//Use the local strategy to login the user
res.status(200).send();
});
}
});
});
app.post("/login", passport.authenticate("local", {//Middleware
successRedirect: "loginsuccess",
failureRedirect: "/loginfail"
}), function(req, res){
});
app.get("/loginsuccess",isLoggedIn, function(req, res)
{
res.status(200).send();
console.log("Login worked");
});
app.get("/loginfail", function(req, res)
{
console.log("Login failed");
res.status(400).send();
});
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
答案1
得分: 1
发现问题,
需要修改登录路由,不再使用 successRedirect
和 failureRedirect
,而是改为使用返回函数。
app.post("/login", passport.authenticate("local", { // 中间件
//console.log("trying login");
//successRedirect: "/",
//failureRedirect: "/fail"
}), function(req, res){
console.log("correct login");
res.status(200).json(req.user);
});
英文:
Found the issue,
Need to alter the login route to not use successRedirect and failure but instead use a return function.
app.post("/login", passport.authenticate("local", {//Middleware
//console.log("trying login");
//successRedirect: "/",
//failureRedirect: "/fail"
}), function(req, res){
console.log("correct login");
res.status(200).json(req.user);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论