How Should a Mobile App Store Login Tokens?
How should a mobile app store authentication tokens?
Use platform-backed secure storage for authentication credentials that must remain on the phone after the app closes. Prefer keeping short-lived access tokens in memory, and persist a refresh token only when the product needs users to stay signed in. Treat storage, token renewal, logout and backup behaviour as one design decision.
Secure storage reduces exposure of stored credentials; it does not make a session impossible to steal or misuse on a compromised device. Android's own Keystore documentation notes that an attacker who compromises the app or the operating system may be able to use stored keys, even though the key material cannot be extracted.
What is the difference between access and refresh tokens?
An access token lets the app make authorised requests. A refresh token lets it request a replacement access token without asking the user to sign in again. Their exact permissions, expiry and renewal rules depend on the authentication service. The OAuth 2.0 standard, RFC 6749, defines both roles and leaves issuing a refresh token to the authorisation server.
For a first release, ask whether staying signed in is necessary. A product used occasionally may accept a fresh sign-in. An app opened throughout the day may need a persistent session. Make that choice while deciding how to scope an app MVP.
Use this as a starting policy:
| Item | Recommended handling | Product decision |
|---|---|---|
| Access token | Keep in memory where practical | How will the app recover after a restart? |
| Refresh token | Persist in platform-backed secure storage when needed | How long should the session remain renewable? |
| Account password | Avoid retaining it after sign-in | Does the chosen sign-in flow require the app to handle it? |
| Signing keys or shared service secrets | Keep outside the distributed app | Which operations belong on the server? |
| Cached account data | Give it a separate storage and cleanup policy | What may remain available offline? |
Do not assume an access token must be saved simply because the refresh token is saved. Document why each credential needs to persist.
What does platform-backed secure storage mean?
It means using the operating system's protected credential or cryptographic facilities through an appropriate storage implementation.
Check the details behind that label. Ask whether the platform protects the token itself or only a key used to encrypt it.
For Android, Flutter or React Native, ask the developer to explain:
- Which platform facility protects the credential?
- Where is the encrypted token held?
- When can the app read it: while locked, after unlocking, or after an additional user check?
- What happens if the protected key becomes unavailable?
- What enters a backup?
- What does the integration do when secure storage fails?
Reject a fallback that silently writes credentials into an ordinary preferences file. If secure persistence fails, design a clear recovery path, such as asking the user to sign in again.
How should short-lived access tokens and refresh work?
Choose access-token lifetimes with the authentication service and the consequences of misuse in mind. Shorter validity limits how long a stolen access token remains useful, but does not itself invalidate a stolen refresh token. The OAuth 2.0 Security Best Current Practice (RFC 9700) describes short-lived access tokens as a way to reduce the impact of a leaked token.
Design one renewal flow that all requests share:
- Obtain an access token after sign-in or successful refresh.
- Use it for authorised requests.
- Renew when required by the service's expiry or error rules.
- Save any replacement refresh credential securely.
- Return to sign-in when renewal is definitively rejected.
Separate an unavailable network from a rejected credential. A temporary connection failure should lead to an appropriate retry or offline state. A confirmed invalid session should lead to sign-in.
Avoid allowing several requests to launch competing refresh attempts. Where the service rotates refresh tokens, coordinate renewal and replacement so requests do not keep using an obsolete credential. Under rotation, as RFC 9700 describes it, each refresh invalidates the previous refresh token.
Also set a retry limit. A request that still fails after renewal should produce a useful result instead of repeating indefinitely.
What should happen when the user logs out?
Define logout as both local cleanup and a server session decision.
Locally, remove persisted tokens, clear access tokens from memory, and stop queued work from using the old session. Decide which cached records, downloaded files and account details must also be removed, especially on shared phones.
For the server, use the authentication service's supported revocation or session-ending mechanism. Deleting a token from one phone does not revoke a copy held elsewhere. Some access tokens can remain valid until expiry unless the server supports an additional invalidation check. The token revocation standard, RFC 7009, notes that access tokens are not necessarily invalidated at once when a refresh token is revoked.
Offer “sign out on this device” and “sign out everywhere” only if the service supports their stated behaviour.
Plan offline logout too. Clear local credentials immediately, then explain the limits of any server action that could not complete. Prevent an in-progress refresh response from restoring credentials after the user has signed out.
Should login tokens be included in backups?
Make backup behaviour an explicit requirement. Do not assume that encrypted storage automatically excludes credentials from backup or prevents migration to another device.
Backup, restore and device-transfer behaviour depend on the platform, protection settings and storage integration. On Android, the Auto Backup guide explains how to exclude files from backup.
A useful default requirement is: restoring the app should not unexpectedly restore an authenticated session.
Ask the developer to verify backup exclusions and credential protection settings. Test restoration separately from an ordinary app restart. Also test reinstall behaviour; do not rely on uninstalling the app as the only way to clear a session.
If restored credentials cannot be read, the app should recover cleanly and request sign-in.
How do you keep tokens out of logs and reports?
Set a rule that credentials must never appear in logs, analytics events, crash reports or support attachments.
Review request headers, response bodies, network debugging tools and error handling. Tokens can be exposed through diagnostic collection even when their persistent storage is protected. Android's security guidance warns that on-device logs are a shared resource and that careless logging can leak user data.
Record the operation and failure category instead of the credential. For example, “session renewal rejected” is more useful for routine support than a full authentication response.
Review third-party reporting settings as part of release testing. Redact sensitive values before collection, and include the resulting reports in the checks described in how to test an Android app before release.
What are the common mistakes?
Using ordinary local storage for convenience. Require a documented, platform-backed protection path for persisted credentials.
Treating encryption as the whole solution. Include key handling, refresh, backup, logout and diagnostic collection in the design.
Embedding a shared secret in the app. A distributed mobile app cannot reliably keep an embedded shared secret confidential from someone who controls a copy. Keep privileged service credentials on the server. The OAuth guidance for native apps, RFC 8252, says a secret shipped in an app to many users should not be treated as confidential.
Trusting the phone to enforce permissions. Require the server to authorise sensitive requests independently of what the interface allows.
Promising protection on every device. On a compromised device, an attacker may be able to observe credentials during use or make requests through the running app. Secure storage is one layer of protection.
What should you verify before release?
Use this checklist as acceptance criteria:
- Restarting the app follows the intended stay-signed-in policy.
- Expired access tokens trigger controlled renewal.
- Rejected refresh credentials return the user to sign-in.
- Network failures do not cause endless retries.
- Concurrent requests share the renewal flow.
- Logout clears credentials and prevents late responses from restoring them.
- Switching accounts does not show the previous account's private data.
- Backup and restoration follow the documented session policy.
- Logs, crash reports and support exports contain no credentials.
Repeat relevant checks when authentication dependencies or platform behaviour change. Include that work in app maintenance after launch.
Frequently asked questions
Should every access token stay only in memory?
Prefer that approach where practical. If a requirement calls for persistence, document it and use platform-backed secure storage with a clear expiry and cleanup policy.
Does secure storage replace server-side security?
No. Require the server to validate credentials and enforce permissions on each protected operation.
Should opening secure storage always require a user check?
Choose this deliberately. An additional unlock step may suit sensitive actions, while background work needs a compatible access policy.
Can the app promise that tokens are impossible to steal?
No. Describe the protections you implement and their limits. Avoid an absolute security promise.
Planning sign-in for a mobile app? Our app development services can help define session behaviour, storage and release checks before implementation.