title: Correct way to set server side cookies for sessions
body: <p>I have following function that is called on the server when user hits certain endpoint, for example <code>/api/login</code> I handle it on the server as I want to set serverside cookie to store authentication token. At the moment it looks like this:</p>
  
  <pre><code>const createSession = (req, res, url) =&gt; {
    const apiCall = request.post(url, (err, response, body) =&gt; { // eslint-disable-line
      if (err) return res.sendStatus(500);
      else if (response.statusCode !== 200) return res.status(response.statusCode).send(body);
      const data = JSON.parse(body);
      const options = { path: '/', hostOnly: true, httpOnly: true, maxAge: 3300000 }; // 55 min expiration
      if (secrets.DOMAIN !== 'localhost') options.secure = true;
      res.cookie('SESSION_TOKEN', data.token, options);
      res.cookie('SESSION_EMAIL', data.email, options);
      res.status(200).send(body);
    });
    req.pipe(apiCall);
  };
  </code></pre>
  
  <p>and it works, but I find it very very verbose and am not sure if there is a better way to handle this, maybe something else besides the request library? I was trying to figure this out using node's http, but never could.</p>
  
  <p>Here is example of using this function in my express server:</p>
  
  <pre><code>app.post('/api/login', (req, res) =&gt; {
    const url = `${secrets.API_HOST}/login`;
    createSession(req, res, url);
  });
  </code></pre>