I have a very simple use case that I cannot get to work. Any ideas what I might be missing?
non-authenticated users can create a post (via an ajax request using vuejs - that works with no problems)
I put the post.id on the user session (that works, too - I can verify that the session key is there)
but when user browses to another web route the session key is not available...
In a non-api setup I would just store the key in the session and then check against the session to verify the user is the owner of the post. I have that working, too.
Are API and WEB session handled as different sessions eventhough they come from same browser?
What can I do to 'keep' data for a user when browsing around?
You need to understand that sessions are not authentication.
You can have a session and not be logged in.
If you log in through the web interface and then try and make a call to the API then it will not recognise you as being the same person because as advised on the other thread API does not use sessions.
Your API calls should be single instances. They know nothing about what has gone before. They leave nothing that is not persisted in the database or on disk.
JavaScript has session data too. It stored on the client side. If you ever need to use token auth for apis, that's where you can store the token and maybe user's name to display on the menu bar.
If you're really getting into apis you need to learn it.
Jeffery did a lesson awhile ago about guest users. Check that out.
Currently trying to get down @SaeedPrez suggestion with cookies.
It seems to be possible solution that might eventually work great - with one problem that I havn't solved yet.
I can push a key->value to the cookie when making a JSON response with ->withCookie('name', 'value', 'minutes')
but by default cookie does NOT get encrypted on API routes (only on web routes)
so when reading the non-encyrpted cookie value on web routes it returns null (of course as laravel tries to decrypt it)
if I apply the EncryptCookies middleware to the api MiddlewareGroup it does work but on some pages a logged in user gets logged out automatically with the Session expired message. (it is a Spark installation)
This brings up a couple of questions:
can I manually encrypt the cookie (as an overwrite of what the middleware does)?
is there a reason why cookies are not encrypted on json responses by default (laravel docs say that cookies are always encrypted)?