TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What is the correct way of session handling in web applications?

1 点作者 sunilkumarc将近 10 年前
Hi Hackers,<p>I&#x27;m a naive web developer who is trying to build a small web application using Node.js and React.js. Currently I&#x27;m stuck at session handling for the chosen technology stack. I have seen some examples(One example : https:&#x2F;&#x2F;github.com&#x2F;rdegges&#x2F;svcc-auth ) which use Node.js for the back end and Jade template engine for the front end. In such applications, sessions are being handled only the server side. I&#x27;m facing difficulties in doing the same thing with Node.js and React.js combination because I&#x27;m handling the routing on the client side using react router.<p>I&#x27;m a bit confused about session handling in web applications. So, I wanted to know what a typical session handling architecture looks like in web applications and what is the correct way (best way) to implement this for Node.js + React.js combination.<p>Any links&#x2F;resources&#x2F;comments are highly appreciated.

1 comment

lsiunsuex将近 10 年前
I&#x27;ve only this week began working with React, so I can&#x27;t speak for that specifically, but sessions generally have the same idea across most languages, IMO.<p>A session is nothing more then a handful of variables and values stored somewhere specific to the user that can be passed back to the server, a query of sorts ran using those values and an output provided.<p>(generally speaking)<p>In PHP, a PHPSESSID generally gets stored on the users machine in a cookie when a user visits a page where session_start() has been executed. That ID corresponds to an array ( $_SESSION ) on the server where for example user_id, name, email, might be set and used to generate this query with the query looking something like (very generic) select * from users where id=$_SESSION[&#x27;user_id&#x27;]<p>Your using NodeJS which means your probably using a document store like Mongo so you can&#x27;t really do queries in the traditional sense, but you can request variable documents<p>In a recent AngularJS &#x2F; Firebase app I built, I use localstorage service to store non-critical information - id, name, email, etc... NEVER the password. Name and email are for presentation - when a user loads a page, it&#x27;s nice for the system to show them who they are - but user_id is what gets passed back to Firebase to do the lookup so in the case of Firebase the &quot;query&quot; is site.firebaseio.com&#x2F;users&#x2F;user_id - this will spit out whatever you have stored in &#x2F;users&#x2F;user_id be it chat history, email address, etc...<p>Could someone modify localstorage variables? yeah probably - but that&#x27;s why on the server side (your NodeJS) your gonna check the incoming variable, make sure it&#x27;s nothing malicious and pass it into the DB and in the case of Firebase, you can setup access rules to further limit who has access to what.<p>I&#x27;d assume a localstorageservice is available to React or something similar. It would be a good place to start.<p>And NEVER store sensative information in a cookie &#x2F; session &#x2F; localstorage, including address info or CC info.<p>(2 cents, I may be completely absolutely wrong)