Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to manually save session from ctx.session? #80

Closed
niftylettuce opened this issue Feb 28, 2016 · 5 comments
Closed

How to manually save session from ctx.session? #80

niftylettuce opened this issue Feb 28, 2016 · 5 comments

Comments

@niftylettuce
Copy link
Contributor

e.g. I want to save session in my error handler middleware, how do I do that?

@tbenst
Copy link

tbenst commented Jun 8, 2016

I'm having a similar issue--unable to save the session when using a socket.io listener

Can read and write:

router.get('/count', ctx => {
    var session = ctx.session;
    session.count = session.count || 0;
    session.count++;
    ctx.body = session.count;
})

Can only read:

io.on('test', (ctx, data) => {
    console.log('test get count', ctx.session.count)
    var session = ctx.session;
    session.count = session.count || 0;
    session.count++;
})

@tbenst
Copy link

tbenst commented Jun 8, 2016

Edit: turns out this has a simple fix. The second fetch will work. Still looking for socket.io workaround

Just as a warning for others, it appears that koa-generic-session does not allow modification to session from a fetch call.

browser:

fetch('/count', {
    method: 'POST',
    headers: {
        count: 1,
    }
})
fetch('/count', {
    method: 'POST',
    headers: {
        count: 2,
    },
    credentials: 'include'
})

server:

router.post('/count', (ctx) => {
    var session = ctx.session
    console.log('sesh1', ctx.session)
    session.count = session.count || 0
    session.count = session.count + ctx.request.header.count
    console.log('sesh2', ctx.session)
    ctx.status = 200   
});

@hedgerh
Copy link

hedgerh commented Jul 3, 2016

im having the same read only issue in websockets while using koa-session and koa-websocket. tried switching to generic-session using a redis store and had the same issue.

@tbenst did you ever find a solution to this?

@tbenst
Copy link

tbenst commented Jul 4, 2016

@hedgerh haven't tried it yet but this appears to outline a solution: http://stackoverflow.com/questions/26643370/get-user-id-socket-io-passport-koa.

If you do try it, please do post if it works and/or the code, cheers!

Edit: also, if you only need to read this works: LnsooXD/koa-socket-session#1 (comment). May be able to modify this code for writing to session, haven't looked into it yet

@niftylettuce
Copy link
Contributor Author

Closed per #94 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants