koajs / koajs/session

bug/question: session not being initialized from external when retrieving

Open
#206 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
908
Forks
115
PR merge metrics
No merged PRs in 30d

Description

https://github.com/koajs/session/blob/6.1.0/lib/context.js#L38

On the linked line from the file above, it's either create new session or initialized from cookie when retrieving a session i.e `ctx.session.something`.

Why does it not support initializing from external as well (on this specific part of the code. external init is being called somewhere else)?

Is this intended?

An example where this might happen is the following:

```
const response = request(koaApp).post('/give-me-session');
const cookie = extractCookieFromResponse(response);
const websocket = new WebSocket('ws:///ws/use-my-session');

// Websocket Handler
function websocketHandler(req) {
const ctx = koaApp.createContext(req);
const authenticated = ctx.session.userId;
}
```

If I were to use cookies, this would work because of the highlighted line above but otherwise it's going to be a new session.

Is this intended? Is this wrong?

Workaround would be:

```
async function websocketHandler(req) {
const ctx = koaApp.createContext(req);
const sessionId = ctx.cookies.get('koa.sess');
const sessionData = await koaSessionStore.get(sessionId);
}
```

Contributor guide

Open the contributing guide

Research direction

Read lib/context.js at line 38 and trace where external session initialization is invoked elsewhere. Compare the cookie and WebSocket request paths in the example, then inspect related session tests and the documented middleware behavior. Done means the intended behavior is established and covered by documentation or a regression test if a change is required.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
authentication, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.