Wednesday 26 February 2020

Can someone explain the node sessions to me?

Hi all! Probably a really dumb question.I don't have any expirience with server based application development, except for maybe some general things. Previously I worked with JS for about a year and a half.So we have this legacy system, which I used to receive and send http requests to via REST in JS. It only accepts basic HTTP auth and returns JWTs back to store in a cookie (I stored them in a global variables, yeah, I know) and make requests with. Since all JS is client-side and my apps never had to be bigger than one page I never had to bother with any kind of session control, as it was managed by the legacy system itself.I now have a goal of creating a server-side application. I understand the basic concepts of node, express, app structure, routing, exporting, static files, etc, but what about multiple users using my app?I played with express session and redis for a bit and was able to get it to work. What I have right now is a demo login page and a demo landing page. If user opens /login, a cookie is generated, then user "logs in" and their username is added to req.session.user, then I ask if (req.session.user === 'demo') and if it is, user is allowed to proceed to the landing page, and if not he is redirected back to login. Is this correct? I feel like it's not...Especially the req part. :(As of now I have the following ideas about the procedure I need:1) Serve static login page to user, on submit grab user and password values and send them to the legacy system via AJAX or http:2) Get a token back, store it in a res.cookie;3) Use it to make GET only request to the legacy system (for now);4) Get the JSON back, render it with handlebars (for ex) and... Then what? Will this part be managed automatically? Currently I have this thing where I can see previous user's Information when I log in. I don't exactly understand how does Express distinguish between users and what to serve to whom. If I get data from our system with JWT in a cookie will it be rendered differently for each user because of his individual session? I feel like a moron but I just can't wrap my head around it all. Please help!Thank you!

Submitted February 26, 2020 at 09:16PM by buttermatter92

No comments:

Post a Comment