Search code examples
node.jsexpressrequestexpress-session

Express Session and Request Modules


In my website, everything server-side is stored in sessions of express-session. But I can't understand why, when I make an HTTP request with request module, the req.session parameter isn't within the request. I mean, follow the comments :

app.get('/prefix', async function(req, res) {
  console.log(req.session.login);
  // There ^ the req.session.login is true, and so it works

  if (req.session.login == false || req.session.login == null) return;
    var options = {
          url: 'https://bot-dreamsub.glitch.me/getPermission',
          json: true,
          jar: true,
          withCredentials: true
        }
  request(options, async function(err, res, json) {
    if (err) {
      throw err;
    }
    console.log(json);
    if (json == true) {
      res.sendFile(__dirname + '/prefix/prefix.html');
    } else {
      return;
    }
  });
});

app.get('/getPermission', function(req, res) {
  console.log(req.session.login);
  // There ^ the req.session.login is undefined, and so it sends null to the user

  try {
    if (req.session.login == false || req.session.login == undefined) {
      res.send(null);
    } else {
      // some other code
    }
  } catch(err) {
    console.log(err);
  };
});

I don't know why request doesn't send sessions within the HTTP request even with

withCredentials: true

What can I do to accomplish it?


Solution

  • An express-session works by setting a cookie in the client's browser that made the original request. Future requests with that same cookie will offer access to that same session. When you do request() yourself from your server, you aren't presenting the same cookie that came in with the original /prefix request so you won't have access to the same session.

    Since it appears you are just trying to use request() to call your own server, I'd suggest you just use a function call and pass the original req.session to that function call so that you will have it available.

    You then use normal code factoring to factor out some common code between your /getPermissions route and what you want to use in your /prefix route so that they can both use and share a common function that you pass the current req and res to. Then you don't need to solve this cookie issue because you'll already have the right req object and thus the correct req.session in this factored common function.

    Alternatively, you could build the right cookie and send that with your request() so that it will appear to be coming from the original browser that has the cookie (and thus session) that you want, but that's kind of the long way to do things when you already have the req.session you want and you could just pass it in a function call rather than start all over and try to simulate a cookie that will get you to the right session.

    I don't know why request doesn't send sessions within the HTTP request even with

    First off, session aren't sent with a request. Cookies are. Your server then uses the cookie to find the right session object.

    Your call to request() does not have the right cookie in the cookie jar you use so when that requests gets to your server, it isn't able to find the right session object. So, when the request is received by your server, it appears to be coming from a different client that does not yet have a session so a new cookie and a new session are probably created for it.


    FYI, if also looks like you may be confusing two definitions of res in your request() call. There's a res defined as an argument in this app.get('/prefix', async function(req, res) { and then you have a separate res in request(options, async function(err, res, json) { that will override the previous one in that scope. It appears to me when you do res.sendFile(__dirname + '/prefix/prefix.html');, you are probably using the wrong res. Probably the best way to solve this is to not use request() at all as suggested above (using a function call to your own server). But, if you were going to still use request(), then you need to name the two res arguments differently so you can still access them both and can use the correct one for your situation.