How Should a Flutter App Store Tokens Securely on Phones? — Security & GDPR article by Rui Codex

To store tokens securely in Flutter, keep refresh tokens in the platform's secure storage, the iOS Keychain or the Android Keystore, usually reached through a plugin such as flutter_secure_storage. Keep access tokens short-lived and in memory where possible, rotate refresh tokens, and end sessions on the server as well as on the device.

Key takeaways

  • Tokens belong in the platform's secure storage, not in plain preferences or files.
  • The iOS Keychain and Android Keystore protect secrets differently, so test both platforms.
  • Short-lived access tokens limit the damage from a leaked token.
  • Session expiry only works when the server enforces it and logout revokes tokens.

Why do tokens need protection on a phone?

A token is a credential. Whoever holds a valid access or refresh token can act as the user until the token expires or is revoked, so it deserves the same care as a password. The OAuth 2.0 framework separates access tokens from refresh tokens for this reason, and the specification describes how each is used, as set out in RFC 6749, The OAuth 2.0 Authorization Framework.

A mobile app runs on a device that can be lost, shared or compromised. Storage that is readable by other apps, or that lands in a device backup in readable form, turns a small leak into a full account takeover. The question is therefore not only where the token sits, but who can read it and for how long it stays useful.

What do the iOS Keychain and Android Keystore do?

They are the operating system's protected stores for small secrets. The iOS Keychain stores items encrypted and lets the app control access through attributes, as described in Apple's Keychain Services documentation. Android Keystore lets an app create and use cryptographic keys whose raw material is hard to extract, as explained in the Android Keystore documentation.

Both stores are far better than a file in the app's documents folder, but they are not identical. On Android, whether a key is hardware-backed depends on the device, so an engineer should check key attestation rather than assume it. On iOS, Keychain items can persist beyond a reinstall in some configurations, so logout code must delete them explicitly.

Tip: Read the access control flags for each item you store. Choosing when an item is readable, for example only while the device is unlocked, is often the most useful protection you control.

Where should tokens never be stored?

Plain shared preferences, local JSON files and hard-coded constants are the common mistakes. Shared preferences are designed for settings, not secrets, and their contents are easy to read on a rooted or backed-up device. Logging tokens during debugging is another frequent leak that survives into production builds.

Web-style storage such as cookies or local storage inside an embedded web view also deserves caution. Treat any location that other code or other people can read as exposed, and keep the token out of URLs, analytics events and crash reports.

How do you implement secure storage in Flutter?

Use one abstraction that wraps the platform store, so the rest of the app never touches raw secrets. The flutter_secure_storage package, documented on pub.dev, uses the Keychain on iOS and encrypted storage backed by the Android Keystore on Android. Check its platform notes before you adopt it, because options such as accessibility level and Android encryption settings change behaviour.

  1. Store the refresh token only, and keep the access token in memory.
  2. Write through a single repository class that handles read, write and delete.
  3. Delete all stored credentials on logout and on a failed refresh.
  4. Handle platform errors, since the store can be unavailable after some device changes.

Teams that build this across mobile and back end often benefit from a shared design review. Our Flutter app development service page describes how we approach mobile builds, and the security and compliance articles cover related engineering topics.

How should session expiry and refresh work?

Expiry should be enforced by the server, because a client can always ignore its own timers. Issue short-lived access tokens, rotate the refresh token each time it is used, and reject a refresh token that is reused, which usually signals theft. The OWASP session management guidance sets out the principles for idle and absolute timeouts, documented in the OWASP Session Management Cheat Sheet.

Choose two timers with different purposes. An idle timeout ends a session after inactivity on a shared device, and an absolute lifetime limits how long one login can last even if the user is active. Pick values with the business and the risk profile in mind, and document the reasoning so the choice can be reviewed later.

Hypothetical examples

Here are two hypothetical scenarios:

  • An app keeps its refresh token in shared preferences because it was the fastest option during prototyping. A device backup or a rooted phone can expose that value, and nothing on the server reveals the misuse because the token still looks valid.
  • Another app stores the refresh token correctly but never revokes it on logout. A token copied before the user signed out keeps working until it expires, so logout only clears the screen and does not end the session.

Both cases fail for reasons that are easy to test. A simple review of where each value is written, and a test that replays a token after logout, would catch either problem before release.

Checklist before release

  • No tokens in shared preferences, plain files, logs or crash reports.
  • Refresh tokens stored only in the platform secure store, with access controls reviewed.
  • Access tokens short-lived and kept in memory where practical.
  • Refresh tokens rotate, and reuse of an old refresh token is rejected by the server.
  • Logout deletes local credentials and revokes the session on the server.
  • Idle and absolute timeouts are defined, tested and documented.

Security work of this kind needs an engineer who has tested it on real devices. If your team is building a Flutter app that handles logins, you can request a project consultation or email info@ruicodex.com to discuss the design.

Frequently Asked Questions

Is flutter_secure_storage enough to make a Flutter app secure?

It is a strong foundation for keeping secrets out of plain storage, but it does not replace good session design. The server still has to issue short-lived tokens, rotate refresh tokens and revoke sessions on logout. Treat the plugin as one layer in a wider design.

Should the access token also go into secure storage?

Usually the access token can stay in memory because it is short-lived and is re-created from the refresh token when needed. Storing it persistently gives an attacker a longer window if the device is compromised. Keep the refresh token in secure storage and make the access token easy to renew.

What happens to Keychain data when the app is uninstalled?

On iOS, Keychain items can persist after an app is deleted, depending on configuration, so they may survive a reinstall. This means logout and first-launch logic should delete credentials explicitly. Do not assume that removing the app clears its secrets.

How do I know whether Android Keystore keys are hardware-backed?

You can check key attestation, which reports how a key was generated and where it is protected. Support varies by device, so the app should handle both hardware-backed and software-backed cases. Document which behaviour your security model accepts.

Should users be signed out after a period of inactivity?

Yes, an idle timeout is a common control for shared or lost devices. The right duration depends on the app's risk and on how users work, so set it deliberately and test the sign-in flow. Pair it with an absolute lifetime that limits any single login.

Conclusion

Secure token handling in Flutter comes down to three habits. Keep long-lived credentials in the iOS Keychain or Android Keystore through a tested wrapper, keep access tokens short and in memory, and let the server enforce expiry and revocation. Test logout and token reuse on real devices before release, because those paths are where most failures hide. If you want a second engineering review of your authentication design, you can request a project consultation or see our published case studies.

Sources

Tags: Flutter Keychain Android Keystore Token Storage Session Management Mobile Security OAuth Refresh Tokens

Need Help Implementing This?

Our team can help you put these insights into practice. From AI automation to custom software development, we build solutions that deliver real results.

Send a project request