title: Update cookie is not working in nodejs with express
body: <p>I have created a login cookie after login but when I try to update the data in that, it gives me an error</p>
  
  <pre><code>Can't set headers after they are sent.
  </code></pre>
  
  <p>My code is below.</p>
  
  <pre><code>/* Logout to main user. */ /* Not Working  */
  router.get('/logoutToMain', mustBe.authorized('supportuser'), function (req, res) {
      var loginSealed = req.cookies[req.ApplicationCookies.LoginCookie];
      if (loginSealed != undefined) {
          iron.unseal(loginSealed, req.Security.CookiePassword, iron.defaults, function (err, unsealed) {
              if (!err) {
                  unsealed.SupportUser = null;
                  iron.seal(unsealed, req.Security.CookiePassword, iron.defaults, function (err, sealed) {
                      if (!err) {
                          res.cookie(req.ApplicationCookies.LoginCookie, sealed, { expires: new Date(Date.now() + 432000), httpOnly: true, ephemeral: true });
                      }
                  });
                  //res.cookie(req.ApplicationCookies.LoginCookie, unsealed, { expires: new Date(Date.now() - 1000), httpOnly: true });
              }
          });
      }
      res.redirect('/');
  });
  </code></pre>
  
  <p>Although when i remove this cookie, that works. (below code)</p>
  
  <pre><code>/* Logout action. */ /*  Working  */
  router.get('/logout', mustBe.authenticated(), function (req, res) {
      var LoginCookie = req.cookies[req.ApplicationCookies.LoginCookie];
      if (LoginCookie != undefined) {
          res.cookie(req.ApplicationCookies.LoginCookie, null, { expires: new Date(Date.now() - 1000), httpOnly: true });
      }
      res.redirect('/');
  });
  </code></pre>
  
  <p>Below piece of code is creating the cookie for a logged in user</p>
  
  <pre><code>var LoggedinEmployee = {
      EmployeeID : recordset[0][0].EmployeeId,
      EmployerID : recordset[0][0].EmployerId,
      EmployerName : recordset[0][0].EmployerName,
      AccountName : Employer.AccountName,
      EmpLevel : recordset[0][0].EmpLevel,
      EmployeeName : recordset[0][0].EmployeeName,
      CorpId: recordset[0][0].CorpID,
      Login: user.Login,
      Password: user.Password,
      SupportUser: null
  };
  iron.seal(LoggedinEmployee, req.Security.CookiePassword, iron.defaults, function (err, sealed) {
      if (!err) {
          res.cookie(req.ApplicationCookies.LoginCookie, sealed, { expires: new Date(Date.now() + 432000), httpOnly: true, ephemeral: true });
          req.ActionOutput.Status = req.ActionStatus.Success;
          req.ActionOutput.Message = 'Logged In. Redirecting to dashboard...';
          res.send(JSON.stringify(req.ActionOutput));
      } else {
          req.ActionOutput.Status = req.ActionStatus.Error;
          req.ActionOutput.Message = 'System Error';
          res.send(JSON.stringify(req.ActionOutput));
      }
  });
  </code></pre>