koajs / session

Simple session middleware for koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

get SessionId

ZhangDianPeng opened this issue · comments

I get the sessionId by ctx.session._sessCtx.externalKey ,but I want to get it through ctx.sessionId.

Can you provide a use case?

  1. When I want to make a user's login on all browsers invalid,I need to record all the sessionIds of this user.
  2. When I want to batch update some users' session.

Of course, I can get the sessionId from the cookie, just think it is more convenient to get sessionId from kos-session.

You could create a utility function to do that for you, pass the session reference and return the correct object reference. You don't need to get it from the cookie, since the session middleware automatically loads it from the cookie.

There is value to being able to fetch that in a more simple fashion, by binding the sessionId so the Session class instead, since the Session class is mostly a proxy class, with some saving logic.

In any case:

const getSessionId = (ctx) => ctx.session._sessCtx.externalKey
module.exports = Utils = {
    getSessionId
};

...
const sessId = Utils.getSessionId(ctx);

Add too many properties in ctx is not a good idea, especially when you add some properties after the module is stable. It may break people's code. :(

I know this thread is long dead, but I just want to add that session id is super important for the state in OAuth and OpenID Connect flows.