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

    1 answer 1

    Regarding cookies. You have httpOnly:true , which prohibits the client, from within js, from accessing them. If you need to be able to do this, you must change it to false .

    Regarding hang-up - are you sure that the query in the database was successful? If not, is there a reject promise exactly at the error? (in the specifically presented code, I don’t see any reason to hang up like this, return completes each branch).

    Do not store the user's password in the clear (google on bcrypt and similar things). And even more so do not save it in the cookie.


    Upd: By the way, it is possible that the redirect occurs, but the page you are trying to access is simply not given (does not send content or even headings), then you can get such a hangup.

    • Thanks for the advice about the password, yes, I'm sure that the request is successful, again with the same console.log () - UraOs
    • I tried to make false, anyway - the connection is written in the title and nothing happens, it hangs in the connection mode, in the console it reaches the string "Cookies are set" - UraOs
    • I will try to look at the dock to express.js for now, maybe there is some subtlety, try for now, before return res.redirect('/user/'+user.login); but after the if condition, insert a log of something. - NumminorihSF
    • Progled everything out. I noticed a pattern, if instead of 'return res.redirect (' / user / '+ user.login)' insert 'return res.send (' / user / '+ user.login)', then the line user / MyFirstUser is displayed (depending Of course, from the login, the information changes) - UraOs
    • Well, it means the case in the final page (see. Updates of the answer) - NumminorihSF