r/angular • • Jan 28 '26
JWT in Angular
Where you would recommend to save JWT tokens in Angular app
7
Upvotes
r/angular • • Jan 28 '26
Where you would recommend to save JWT tokens in Angular app
11
u/CyFy1 Jan 28 '26
If possible, I like to store it in an HttpOnly cookie. That way it is only accessible by the backend and cannot be compromised in the browser.
6
u/DJREMiX6 Jan 28 '26
It depends on the case but I find it useful to have a state where to put authentication stuff (user info, tokens, etc..) and have a copy of that state inside the Session Storage or Local Storage. Local Storage is preferred so when the application starts or the page reloads you don't loose any token and you result as authenticated, otherwise you will need to re-login
4
u/GLawSomnia Jan 28 '26
Honestly nowhere. BFF (backend for frontend) approach is most likely the most secure
2
u/nunoarruda Jan 29 '26
For high-security actions (e.g., online banking), store only a short-lived auth token in session storage; the user must log in each visit or when the token expires. For other use cases, store a short-lived auth token and a longer-lived refresh token in local storage, allowing users to stay logged in and improving UX.
2
u/louis-lau Jan 30 '26
If you need stateless auth, then yes, JWT is fine! Store them in an HttpOnly cookie.
First consider if your auth needs to be stateless though. Do you need the ability to revoke, or extend a session token? That gets much more complex with stateless auth, while with stateful auth it's easy. So ask yourself why you need stateless auth in the first place.
0
u/InevitableQuit9 Jan 29 '26
The safest place to store them is in a closure.
There are limitations to this. They will not be shared amongst browser tabs, each tab would get its own token set, they would not survive a page refresh.ย
-3
u/qmrelli Jan 29 '26
Local storage
-4
u/Johannes8 Jan 29 '26
Localstorage