英文:
Is storing data in localStorage or sesssionStorage the only way to persist data in react?
问题
我正在使用React和Symfony构建电子商务网站。我试图使用Context API保存用户信息,但问题是在页面刷新时数据不会保留。我该如何实现这一点?
英文:
I'm building an e-commerce website using react and symfony.
I'm tryin to save a user's info, and I'm using context api to do that, but the issue is that data isn't persisting when page is refreshed. How can I achieve this?
答案1
得分: 1
以下是翻译好的内容:
不建议将任何敏感信息存储在浏览器存储中,因为这会为漏洞敞开大门。
用户信息 - 和其他数据 - 由后端提供。你可以创建一个仅限 HTTP 的 cookie来保存用户令牌,并创建一个提供所需用户信息的端点。一旦应用程序启动,该请求将发送一次。如果 cookie 有效,端点将返回用户信息,然后你可以在应用程序中使用这些信息,而无需将任何内容存储在浏览器存储中。
每次刷新页面时,你都会发送该请求,并获取用户信息。
你所询问的主题涉及到不同部分,其实现方式根据你使用的后端技术栈而异。我建议你查看这个视频或其他资源,以了解如何实施。
英文:
It's highly discouraged to store any senstive information in the browser storage because it opens the door for vulnerabilities.
The user information -and other data- are provided by the backend. One way you can do it is to create an HTTP-Only cookie that holds the user token and create an endpoint that provides the needed user information. Once the application starts, that request is sent once. If the cookie is valid, the endpoint will return the user information then you can use these information in your application without storing anything in the browser storage.
Every time the page is refreshed, you send that request and it will get the user information.
The topic you are asking about tackles different parts and its implementation differs according to the backend stack that you are using. I would suggest you check this video or other resource to understand how to implement it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论