“nodejs android studio 和 passport js 登录 404 错误”

huangapple go评论82阅读模式
英文:

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

发现问题,

需要修改登录路由,不再使用 successRedirectfailureRedirect,而是改为使用返回函数。

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);

});

huangapple
  • 本文由 发表于 2020年7月24日 20:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63073481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定