title: Getting different sessionId from the same browser
body: <p>I'm using node express-session to try and have persistent session in the angular app.
  I would like to clear the session whenever I close the browser window.
  The problem is that the session id that is sent through the request of the browser when trying to clearSessionCookie (in the req parameter in the middleware function) is different that the session id that is sent when using the getSession - <strong>same browser - different session id on different requests</strong>.
  The client event that trigger it in the app.component is:</p>
  
  <pre><code>@HostListener('window:beforeunload', ['$event'])
  beforeunloadHandler(event) 
  {
    this.sessionSrv.clearSessionCookie().subscribe();
  }
  </code></pre>
  
  <p>The service in the client side that send the requests:</p>
  
  <p>export class sessionService 
      {
          apiPath : string = environment.apiUrl;</p>
  
  <pre><code>    constructor(private http: HttpClient) { }
  
      getSession() : Observable&lt;any&gt; \\this is used to get the session
      {
          var path = `${this.apiPath}/session`;
          const options = { withCredentials: true };
          return this.http.get&lt;any&gt;(path, options).pipe(   
              tap(res=&gt;{ console.log('sessionCheck!'); }),
              catchError(error =&gt;  throwError(error))
          );  
      }
      clearSessionCookie() : Observable&lt;any&gt; 
      {
          var path = `${this.apiPath}/session/clearSessionCookie`;
          const options = { withCredentials: true };
          //return this.http.get(path);
          return this.http.post(path, options);
      }
  }
  </code></pre>
  
  <p>The node server side code responsible for routing the request to the end point:</p>
  
  <p>session.js:</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  var sessionManagement = require('../../middleware/sessionManagement');
  
  router.use("/clearSessionCookie", (req, res, next) =&gt; 
  {
      sessionManagement.clearSessionCookie(req, res, next);
  });
  
  router.use("/", (req, res, next) =&gt; 
  { 
      sessionManagement.getUserSession(req, res, next);
  });
  
  module.exports = router;
  </code></pre>
  
  <p>The node server side relevant functions:
  sessionManagement.js</p>
  
  <pre><code>module.exports = 
  {
      getUserSession : function (req, res)
      {
          if (req.session &amp;&amp; req.session.user)
          {
              res.status(200).json(req.session.user);
          }
          else{
              res.sendStatus(403);
          }
      },
  
  
      clearSessionCookie : function(req, res, next)
      {
          res.clearCookie('user_sid');  
          req.session = null;
          //next();
          //res.status(401);
          res.end();
      }
  }
  </code></pre>