Session handling when using Vue CLI
See original GitHub issueSorry to bother you again, but I have only one open question left after I split my front-end and back-end.
It is session handling. As long as I used Javalin for both side, I enjoyed the out-of-box session handling provided. I extended the context with some session specific fields (thanks to Kotlin extension properties and functions 😉) and all worked well without any manual tweaking. However, as long as I split the two sides, cookie handling become a nightmare. It is because now the app listens on two, different ports (8080 for front-end and 1551 for back-end) on the same host, but the cookies are not heeding port. Therefore, both side sets the JSESSIONID cookie and overrides each others. 😄
As a solution I have to make manual session handling on some degree, but I don’t know how deep I have to delve.
OPTION 1: Use different cookie for the automated Javalin session management This would be the easiest and least-intrusive way. My question whether I can set up Javalin to use different cookie for session marking and pairing than JSESSIONID? If I can, it is clear and I could use it and enjoy the blessings of the built in session management.
OPTION 2: Use my own session management If the above is not possible, I need to create a simple session cache. This needs two things: (1) I have to tell Javalin NOT to create JSESSIONID cookie to avoid overwriting front-end cookie of the same name. (2) I have to send my own cookie when logged in and let the client send it back any time I call the back-end
I have some experiment with the later and I was able to send a cookie (it’s just a POC) as the response of login call:
ctx.cookie(Cookie( "pb-session", ""+Random.nextLong(), sameSite = SameSite.NONE))
but it is not sent back on the next call, which is a fetch of an image (a src
of an img
tag). I can’t figure out what the problem is.
So my question if I could do OPTION 1 and how, and if I can’t, how could I fulfill the two requirements of OPTION 2?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Finally, with a lot of research and try’n’die I figured out I was really almost there. Only one line I missed from login call. Let’s stand here for others to learn:
Reason: AJAX calls only handles cookies when credentials: “include” is also there in the request.
Now it uses my own cookie and handles sessions correctly. (Still need some additional testing, but good so far. 😉)
Thanks for updating the issue with your solution @balage1551 - I labeled it as
INFO
and changed the title to better reflect the issue 😃