Review a generated session store

from Classes
Node 24 advanced 6 min 4 issues to find

Review this generated session store before it is used by an authentication service.

Build an extensible session store whose create method can be registered as a callback, returns summaries without tokens, tolerates missing lookup input, and supports frequent name lookup.

JavaScript
class SessionStore {
  static #nextId = 1;
  #sessions = [];
  constructor(audit) { this.audit = audit; }
  create(userName, token) {
    const session = { id: SessionStore.#nextId++, userName, token, roles: [] };
    this.#sessions.push(session);
    this.audit(`created ${session.id}`);
    return session;
  }
  get sessions() {
    return [...this.#sessions];
  }
  findByUser(name) {
    return this.#sessions.find((session) =>
      session.userName.toLowerCase() === name.toLowerCase(),
    );
  }
}
class AdminSessionStore extends SessionStore {}
const store = new AdminSessionStore(console.log);
const create = store.create;
[['Ada', 'secret-a'], ['Lin', 'secret-b']].map(([name, token]) => create(name, token));
console.log(store.sessions);

generated code is illustrative, not from any one model

Open in playground
Report an error