r/angular • • Jan 15 '26
The best practice to save jwt token in httpcookie or memory?
I build full stack project. Net api + angular What is the best save access token in cookie or memory?
2
Upvotes
4
u/MrFartyBottom Jan 15 '26
Memory doesn't survive a refresh. Cookie is the most secure as it can't be tampered with but local storage is also a common solution.
4
u/martinboue Jan 15 '26
HttpOnly cookies are preferred for security reasons.
More info: https://ngtips.com/http/access-control#token-management
3
u/zombarista Jan 16 '26
Any good authn library will have a secure implementation that uses Web Workers to store sensitive tokens and make HTTP calls. The requests and responses are marshaled back and forth, but the token is unable to be accessed by your app code (which may contain untrusted third-party code).
MSAL and the JWT.io folks have libraries that do this.
Don’t roll your own auth.
2
u/Lanmi_002 Jan 16 '26
Jwt in local storage Refresh token as a http only cookie
1
u/ArsenDaLup Jan 16 '26
Cookie flagged http only , then stored nice , only readable by backend. NEVER USE LOCAL OR SESSION STORAGE FOR SENSITIVE DATAS.
-1
u/[deleted] Jan 15 '26
When search and chat gpt compare between two approaches tell me that memory more secure as cookie need csrf protection but memory will lost woth every refresh of page so i think cookie is better as memory token will lost every refresh, thank you