admin管理员组文章数量:1398814
I am building an admin panel where users must complete a two-factor authentication (2FA) process after logging in. The flow works as follows:
User logs in successfully.
They are redirected to the 2FA verification page.
Upon successful 2FA verification, they are redirected to the home page.
To manage this process, I store authentication-related flags in localStorage:
is2FAPending: "Yes" or "No" (Indicates whether the user is in the 2FA process)
is2FAFinish: "Yes" or "No" (Indicates whether the 2FA process is completed)
Once the user successfully enters the 2FA code, I update these values and redirect them to the home page:
localStorage.setItem("is2FAPending", "No");
localStorage.setItem("is2FAFinish", "Yes");
window.location.href = "/home";
The Problem: Since these values are stored in localStorage, a user can manually update them in the browser console (e.g., setting is2FAPending to "No" and is2FAFinish to "Yes") without completing the 2FA process. This allows them to bypass authentication and access the home page.
My Question: How can I prevent users from modifying localStorage values to bypass 2FA? What is the best approach to securely track 2FA authentication without relying on client-side storage?
Possible Solutions I Considered: Move authentication state to HTTP-only cookies or session storage (but not sure how to implement this properly).
Verify 2FA completion from the backend on every protected page load.
Use JWT tokens to track authentication state instead of relying on localStorage.
What would be the best and most secure approach? Any guidance would be greatly appreciated!
I am building an admin panel where users must complete a two-factor authentication (2FA) process after logging in. The flow works as follows:
User logs in successfully.
They are redirected to the 2FA verification page.
Upon successful 2FA verification, they are redirected to the home page.
To manage this process, I store authentication-related flags in localStorage:
is2FAPending: "Yes" or "No" (Indicates whether the user is in the 2FA process)
is2FAFinish: "Yes" or "No" (Indicates whether the 2FA process is completed)
Once the user successfully enters the 2FA code, I update these values and redirect them to the home page:
localStorage.setItem("is2FAPending", "No");
localStorage.setItem("is2FAFinish", "Yes");
window.location.href = "/home";
The Problem: Since these values are stored in localStorage, a user can manually update them in the browser console (e.g., setting is2FAPending to "No" and is2FAFinish to "Yes") without completing the 2FA process. This allows them to bypass authentication and access the home page.
My Question: How can I prevent users from modifying localStorage values to bypass 2FA? What is the best approach to securely track 2FA authentication without relying on client-side storage?
Possible Solutions I Considered: Move authentication state to HTTP-only cookies or session storage (but not sure how to implement this properly).
Verify 2FA completion from the backend on every protected page load.
Use JWT tokens to track authentication state instead of relying on localStorage.
What would be the best and most secure approach? Any guidance would be greatly appreciated!
Share Improve this question asked Mar 26 at 22:41 Vugar abdurahmanovVugar abdurahmanov 11 bronze badge 3 |1 Answer
Reset to default 0I would strongly recommend moving the authentication state away from client-side storage like localStorage
.
Client-side storage like localStorage
and sessionStorage
can be manipulated easily by users, which is why relying on them for sensitive information like authentication status is not secure.
My opinion, if you're starting, try implementing cookies
for 2FA AuthManagement
.
After successful completion of first layer authentication, set a flag
like 'is2FAPending'
and set it to true
in initialState
. This should be set in the backend cookies. Then redirect to the 2FA Page.
I don't know the backend technology you're using, but if you are using Express.js
, you can set the cookie
inside session()
.
You can refer this guide to setup:
https://www.geeksfeeks./how-to-manage-sessions-and-cookies-in-express-js/
After successful completion of 2FA set the flag
to false
and redirect to a Protected Layout which sends a request and check the cookie/session
for the state
of is2FAPending == false/true
Hope this answers your question!
本文标签: reactjsPrevent LocalStorage Manipulation for 2FA Authentication in Admin PanelStack Overflow
版权声明:本文标题:reactjs - Prevent LocalStorage Manipulation for 2FA Authentication in Admin Panel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744122313a2591797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/home
to the server, and if that page is restricted, then the backend needs to determine if the user is authenticated/authorized before returning that page to the user. Keeping the authentication entirely client-side means that the server has no way of knowing whether the user is logged in. So if the server always returns the data that's been requested, regardless of authentication, what then would be the point of logging in at al? – David Commented Mar 27 at 11:48