How do you store tokens securely in a web or mobile app?
Ask by ICSM Computer
Updated 10 Jun 2025
Storing authentication tokens (like JWTs, OAuth access tokens, refresh tokens, or API keys) securely is crucial to preventing unauthorized access and token theft.
Here’s a breakdown based on platform:
1. Web Apps (Browser-based)
Tokens to Store
Best Practices
Memory(JavaScript variable)HttpOnly CookieSameSite,Secure,HttpOnly.localStorage/sessionStorageRecommendation
HttpOnlysecure cookies for session-based tokens (with CORS configured properly).localStorageunless absolutely necessary and XSS risk is well mitigated.2. Mobile Apps (iOS, Android, etc.)
Best Practices
flutter_secure_storage,react-native-keychainTips
3. Desktop Apps (Electron, WPF, etc.)
keytar)ProtectedData)Example in C#:
Bonus: Refresh Token Strategy
For SPAs or mobile apps:
What Not to Do
localStorageorsessionStorageif you care about XSS protection.Summary