question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Session handling when using Vue CLI

See original GitHub issue

Sorry 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
balage1551commented, Nov 13, 2021

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:

     const requestOptions = {
            method: "POST",
            credentials: "include",   // THIS IS WHAT I MISSED
            headers: {"Content-Type": "application/json"},
            body: JSON.stringify(data),
            mode: "cors"
        }
        fetch(url, requestOptions, callback)

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. 😉)

0reactions
tipsycommented, Nov 14, 2021

Thanks for updating the issue with your solution @balage1551 - I labeled it as INFO and changed the title to better reflect the issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage Sessions Over HTTPS With Node.js And Vue.js
Learn how to manage sessions between a Node.js backend and a Vue.js frontend that exist on different origins and are secured with HTTPS....
Read more >
How do I handle login/registration and sessions with vue ...
How do I create a session when a user is authenticated, what am I supposed to do with a successful login/register response to...
Read more >
How to save users sessions with VueJS? - Stack Overflow
I am trying to figure out how to save users session when they log out. e.g. multiple shopping carts, transactions ...
Read more >
vue-session - npm
A simplistic session plugin for VueJS backed by SessionStorage. ... Start using vue-session in your project by running `npm i vue-session`.
Read more >
Storing Session Data with the Vue Session Plugin
Conclusion. Vue Session is an easy to use library for storing sessions within a Vue app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found