I want to login to my account. I enter the username and password, after which a request to the database with the corresponding username is made in the post request, if we find such a username, then we match the passwords, if we match, then if the "Remember" checkbox is ticked, we assign a cookie. I assign them inside the asynchronous function (as I understand it). After that, I want to go to this page. This is where the problem arises. Sometimes it goes to that page, and sometimes it does not go (more often, just the page shows what is loading, but it remains to be "hanging"). In cases where it is “lucky,” and sometimes goes to a page, it defines the assigned cookies as console.log() defined by console.log() , although these cookies are saved in the browser (it looked in the settings). How to solve this problem, so that it goes correctly to the page, and the cookie determines.
This is the query on which the user is searched:
router.post('/', function(req, res, next) { //функция, которая возвращает состояние запроса function getPromiseOfQueryForUser(objModel,infoForSearch){ var promise = objModel.findOne({login :infoForSearch}).exec(); return promise; } var Users = models.Users; //состояние логина var promiseLogin = getPromiseOfQueryForUser(Users,req.body.login); promiseLogin.then(function(user){ if ( user.password == req.body.password ){ if (req.body.rememberSignIn) {//если нажата галочка 'Запомнить' //назначить куки var minute = 10 * 1000; res.cookie('user', { login:user.login, password:user.password}, { maxAge: minute, httpOnly:true}); console.log("Cookies is set!"); } return res.redirect('/user/'+user.login); } else { return res.redirect("/signin"); } }, function(err){ //произошла ошибка при анализе состояния console.log(err); return res.redirect("/signin"); }); });