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

Session lost on redirect to other controller #771

Closed
silencechow opened this issue Jan 24, 2018 · 11 comments
Closed

Session lost on redirect to other controller #771

silencechow opened this issue Jan 24, 2018 · 11 comments
Assignees

Comments

@silencechow
Copy link

silencechow commented Jan 24, 2018

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

@touskar
Copy link

touskar commented Jan 24, 2018

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?

@thetutlage
Copy link
Member

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

@silencechow
Copy link
Author

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

@thetutlage
Copy link
Member

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

@thetutlage thetutlage self-assigned this Jan 25, 2018
@silencechow
Copy link
Author

It's a form submit from other view.

@thetutlage
Copy link
Member

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

@touskar
Copy link

touskar commented Jan 25, 2018

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') )
}

@silencechow
Copy link
Author

silencechow commented Jan 26, 2018

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

@touskar
Copy link

touskar commented Jan 26, 2018

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') )
}

@thetutlage
Copy link
Member

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

@lock
Copy link

lock bot commented Mar 11, 2020

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 lock bot locked and limited conversation to collaborators Mar 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants