nodejs android studio 和 passport js 登录 404 错误
nodejs android studio and passport js login 404 error
我在尝试通过我的 android 应用程序登录我的节点 js 本地服务器时遇到问题。
每次尝试登录时,我都会收到 404 错误。我可以使用 Post 路由很好地注册用户,并且可以在我的 mongodb 数据库中使用用户名和密码(散列+加盐)查看它。
但是当我使用我的凭据(用户名和密码)正确登录时,我总是收到 404 错误。我认为这意味着我的登录路径不正确。
登录我的移动应用程序 posts 到登录路由并发送用户名和密码。
但是在 运行 我的成功和失败路由上没有 post 它们相应的控制台日志,我得到 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(){//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");
}
找到问题,
需要更改登录路由以不使用 successRedirect 和 failure,而是使用 return 函数。
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);
});
我在尝试通过我的 android 应用程序登录我的节点 js 本地服务器时遇到问题。 每次尝试登录时,我都会收到 404 错误。我可以使用 Post 路由很好地注册用户,并且可以在我的 mongodb 数据库中使用用户名和密码(散列+加盐)查看它。
但是当我使用我的凭据(用户名和密码)正确登录时,我总是收到 404 错误。我认为这意味着我的登录路径不正确。
登录我的移动应用程序 posts 到登录路由并发送用户名和密码。 但是在 运行 我的成功和失败路由上没有 post 它们相应的控制台日志,我得到 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(){//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");
}
找到问题,
需要更改登录路由以不使用 successRedirect 和 failure,而是使用 return 函数。
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);
});