Session lost on redirect to other controller
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
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?
Mind sharing a complete code example. Also please do format your code blocks properly. Since it is hard to read the code otherwise
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
May I know how you visit the /foo/:id/upload URL?
It's a form submit from other view.
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
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') )
}
I am using cookie as session drive.
And it's work for me If I added the code await session.commit() after session.put(...)
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') )
}
There doesn't seems to be any issues with the session. I suggest sharing a repo with minimal code to reproduce the issue
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.