search

Home  >  Q&A  >  body text

node.js - Nodejs login information saving problem

After I log in on one computer, the logged-in user information will be shared on another computer that is not logged in. However, I just store it in a cookie. Logically speaking, the two clients are not mutually exclusive. Don't you want to interfere? Please give me some guidance from God

Login login.js

router.post('/login',function(req, res, next){
    var account=req.body.account;  //账号
    var password=req.body.password; //密码
    var options = {
        method:'post',
        uri:"/login",
        qs:{account:account,password:password},
        headers:{'User-Agent': 'Request-Promise'},
        json: true
    }
    
    http.tp(options,function(error, response, body){
        res.cookie('_user', body.data, {maxAge: 24*60*60 * 1000});  //登录成功后把后台返回的信息保存到cookie
        res.redirect('back');
    })
})

app.js

app.use(function(req, res, next){
  var _user=req.cookies._user;   //获取保存到cookie的用户信息

  res.locals.user =_user;    //发送给浏览器
  res.locals._user =_user;
  res.locals.success = req.flash('success').toString();
  res.locals.error = req.flash('error').toString();
  next();
});

When I save information in this way, won’t the cookie be saved only on the client browser where I log in?

天蓬老师天蓬老师2807 days ago475

reply all(1)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 13:34:05

    The cookie is saved to the server?

    reply
    0
  • Cancelreply