我正在研究"注册"我的网络应用程序的路由。在我的路由中,我使用toLowerCase()函数将用户输入的用户名转换为小写,但这会导致passport.authenticate()方法失败(用户仍然完全注册)并重定向到Unauthorized(401)响应。当我尝试不使用toLowerCase()函数时,它工作得很好。不知道我在这里缺少什么,所以任何帮助将不胜感激!
router.post("/register", isLoggedOut, function(req,res){
var username = req.body.username;
var password = req.body.password;
var confirm_password = req.body.confirm_password;
for (var i = 0; i < emails.length; i++){
if (username.toLowerCase() == emails[i].toLowerCase()){
username = username.toLowerCase();
if (password != confirm_password){
req.flash("error", "Passwords do not match!");
return res.redirect("/register");
} else {
console.log("Password matches! Signing up now");
console.log("Type of username is " + typeof req.body.username);
console.log("Type of username after lowercase: " + typeof req.body.username.toLowerCase());
var newUser = new User({username: username, role: "public"});
User.register(newUser, req.body.password, function(err, user){
if (err){
console.log("There is an error in registration: " + err);
req.flash("error", err.message);
return res.redirect("/register");
}
passport.authenticate("local")(req, res, function(err){
if (err){
console.log("ERROR IN AUTHENTICATE!: " + err);
}
req.flash("success", "Welcome");
return res.redirect("/blogs");
});
});
}
}
else {
if (i == emails.length - 1){
console.log("Email is not in system");
req.flash("error", "Your email is not in our system!");
return res.redirect("/register");
}
}
}
});