express-session has no exported member named 'session'. Did you mean Session?

Viewed 18

so I understand that this should only execute a logout if a user session exists. We log out the logout event only if an authorization header is passed through. If there is an authorization header, we remove the string Bearer from it, then decode it to get the username and supporting log information. I don't understand why I need to check it to be of type function: typeof req.logout === 'function' but at any rate, I need to integrate a req.session.destroy() on it because the code as-is gives me the following error: express-session has no exported member named 'session'. Did you mean Session?

And its not that I have not implemented session(), I have it in the main.ts file here:

function configureApp(app: INestApplication): void {
  const configService = app.get(ConfigServer);
  app.use([session({ secret: process.env.APP_SECRET, cookie: { maxAge: null } }), CorrelationIdMiddleware(), ClientIdMiddleware()]);
console.log('CONFIG SERVER CORS: ', configService.get('cors'));
app.enableCors(configService.get('cors'));
SwaggerModule.setup('swagger', app, apiDoc(app));
}

I tried to integrate the destroying of session:

async logout(req: Request) {
  const token = req.headers?.authorization.substring(7, req.headers?.authorization.length);
  const user = (this._jwtService.decode(token) as IUser) || null;
  req.session.destroy(function (err) {
    if (req.logout && typeof req.logout === 'function') {
      req.logout({ keepSessionInfo: false }, (err) => {
        throw err;
      });
      if (user) {
        this.logger.info({ uid: user._id, org: user.organization._id}, `${user.username}logged out`);
      }
    }
  });
  return '';
}

But I continue to get the same error in terminal. I think the bug may be somewhere in that logout method because if I create a more simple version of that logout like so:

async logout(req: Request) {
  req.session.destroy(function (err) {
    console.log('logged out');
  }
}

I am able to log in, log out and log in again repeatedly without error.

1 Answers

I was able to resolve it with this solution:

async logout(req: Request) {
  const token = req.headers?.authorization.substring(7, req.headers?.authorization.length);
  const user = (this._jwtService.decode(token) as IUser) || null;
    if (req.logout && typeof req.logout === 'function') {
      if (user) {
        this.logger.info({ uid: user._id, org: user.organization._id}, `${user.username}logged out`);
      }
    }
    req.session.destroy(function (err) {
      return '';
    });
}

I can now login->logout->log back in again successfully without the error message in terminal.

Related