One user instance tied to one session dataset

I am trying to use a session to hold a set of data for a user. How do I tie one user to one session? Or does it not work like that? Essentially, how do I make sure one client instance, has one set of session data? I am using React and Node. So far this is my code:

main.ts

app.use(
  session({
    secret: 'secret',
    resave: false,
    saveUninitialized: false,
  }),
);

.service.ts

public async getCachedSelectedClient(
  session: Record<string, any>,
): Promise<string | undefined> {
  const { cachedClientId } = session;
  this.logger.log('cached client id', cachedClientId);
  return cachedClientId;
}

public async setCachedSelectedClient({
  session,
  cachedClientId,
}): Promise<void> {
  if (!cachedClientId) {
    return this.logger.log('No cachedClientId provided');
  }
  session.cachedClientId = cachedClientId;
  this.logger.log('set cached client response', cachedClientId);
}

If I use postman I get the data I set, but when I try getting the data with swagger, I don’t get any data. Does this mean it’s already somehow tied to one request origin point?

Answer

So turns out express-session uses cookies to connect the client to the session. Simple and works out of the box afaik

Leave a Reply

Your email address will not be published. Required fields are marked *