core icon indicating copy to clipboard operation
core copied to clipboard

Session lost on redirect to other controller

Open silencechow opened this issue 8 years ago • 11 comments

Flow: I set a session using await session.put('message', { test: 'TEST' }) in A Controller and return response.redirect( Route.url('xxxxxx' ) to B Controller

Using session.pull('message')in B Controller is null

cookie: {
    httpOnly: true,
    sameSite: false,
    path: '/'
}

in config/session.js

silencechow avatar Jan 24 '18 08:01 silencechow

First, no need to await while putting on session, session values are persisted in bulk when the request ends. Are you using two different domain?

touskar avatar Jan 24 '18 10:01 touskar

Mind sharing a complete code example. Also please do format your code blocks properly. Since it is hard to read the code otherwise

thetutlage avatar Jan 24 '18 11:01 thetutlage

It's redirect to same domain but different controller only Here is the route.js

...
Route
    .get('foo', 'Foos/BarController.index' )
    .as('foo.bar.index')
Route
    .post('foo/:id/upload', 'Foos/QuxController.store' )
    .as('foo.qux.upload')
...

I want to a success message to BarController.index via session

In QuxController store function

async store ({ auth, request, params, response, session }) {
    ...
    await foo.save()

    session.put('message', {
		type: 'Well done!',
		content: `You have stored ${foo.name}.`
    })
    return response.redirect( Route.url('foo.bar.index') )
}

In BarController index function

index ({ view, session }) {
    return view.render('foo.bar.index', {
	message: session.pull('message')
    })
}

But I can get the session if redirect from BarController

silencechow avatar Jan 25 '18 02:01 silencechow

May I know how you visit the /foo/:id/upload URL?

thetutlage avatar Jan 25 '18 05:01 thetutlage

It's a form submit from other view.

silencechow avatar Jan 25 '18 06:01 silencechow

Seems to be working fine for me. Also session.pull gets the value from the session and then removes it. So it will only exists for one time and refreshing the same page will not have the value

thetutlage avatar Jan 25 '18 06:01 thetutlage

What session drivers are you using? Can you manually commit session like that to see if values are persisted in bulk:

async store ({ auth, request, params, response, session }) {
    ...
    await foo.save()

    session.put('message', {
		type: 'Well done!',
		content: `You have stored ${foo.name}.`
    });

    await session.commit();// add this

    return response.redirect( Route.url('foo.bar.index') )
}

touskar avatar Jan 25 '18 08:01 touskar

I am using cookie as session drive. And it's work for me If I added the code await session.commit() after session.put(...)

silencechow avatar Jan 26 '18 02:01 silencechow

Normally the session should be persisted automatically. I wonder if the "Foos QuxController.store" controller is not running before the SessionMiddleware finishes persisting data. Can you confirm that at least one of these two examples works too:

async store ({ auth, request, params, response, session }) {
    ...
    await foo.save()

    session.put('message', {
		type: 'Well done!',
		content: `You have stored ${foo.name}.`
    });

   let wait = (delay) {
    return new Promise(function(resolve) {
        setTimeout(resolve, delay);
    });
   }
    await wait(2000);//add this
    return response.redirect( Route.url('foo.bar.index') )
}

OR

async store ({ auth, request, params, response, session }) {
   /*
   * add this
   */
    response.implicitEnd = true; 
    ...
    await foo.save()

    session.put('message', {
		type: 'Well done!',
		content: `You have stored ${foo.name}.`
    });
    
    return response.redirect( Route.url('foo.bar.index') )
}

touskar avatar Jan 26 '18 02:01 touskar

There doesn't seems to be any issues with the session. I suggest sharing a repo with minimal code to reproduce the issue

thetutlage avatar Jan 27 '18 18:01 thetutlage

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Mar 11 '20 08:03 lock[bot]