Sub topic 9
Reinstalls, Backups and Sign-Out
Assume you know basic Dart async and await, can add Flutter packages, and understand that an authentication token proves a session. This chapter focuses on what happens to stored secrets when the app lifecycle changes.
Secure storage is persistent, but persistence does not mean permanence or portability. The result depends on the operating system, the storage backend, backup settings, and the action your app performs.
What happens during an app reinstall?
iOS Keychain items commonly remain after the app is deleted and installed again. This behavior can surprise users: a newly installed app may still find an old refresh token and sign the user in automatically.
Android behavior is different. Data stored through an Android Keystore-backed implementation is generally removed when the app is uninstalled. However, Android backup and restore can copy encrypted preferences to a new installation. The restored ciphertext may refer to a key that no longer exists. Reading it can then fail.
Treat a restored or unreadable token as invalid. Do not repeatedly retry the same value.
Backups and unreadable data
Encrypted data is tied to encryption keys. A backup can contain the encrypted value without containing the original key. This is why a read operation can throw a PlatformException after a device migration or restore.
Configure Android backup behavior according to your threat model. For an authentication token, excluding the relevant encrypted preferences from backup is often safer than restoring data that cannot be decrypted. Package options and Android manifest settings vary by flutter_secure_storage version, so check the version's current documentation before shipping.
Your repository should convert storage failures into a safe authentication state. Clearing all values is appropriate only when this storage belongs exclusively to the authentication feature.
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class SessionStore {
SessionStore([FlutterSecureStorage? storage])
: _storage = storage ?? const FlutterSecureStorage();
static const _refreshTokenKey = 'refresh_token';
final FlutterSecureStorage _storage;
Future<void> saveRefreshToken(String token) {
return _storage.write(key: _refreshTokenKey, value: token);
}
Future<String?> readRefreshToken() async {
try {
return await _storage.read(key: _refreshTokenKey);
} on PlatformException {
// The value may have been restored without its encryption key.
await _storage.delete(key: _refreshTokenKey);
return null;
}
}
Future<void> signOut() {
return _storage.delete(key: _refreshTokenKey);
}
}Sign-out must clear every session artifact
A refresh token is only one possible session artifact. Your app might also store an access token, device identifier, or user-specific encryption key. Sign-out should remove all values that identify the local session.
If your app supports multiple accounts, do not call deleteAll() unless you intend to remove every account and every unrelated secret. Prefer named keys or a dedicated storage wrapper. After deletion, clear in-memory state too. Otherwise, a screen may continue using an access token that is no longer in secure storage.
A server-side logout or token revocation can complement local deletion. Local deletion protects the device from future use of the token, while server revocation can invalidate a token that has already been copied.
Practical lifecycle rules
- Validate a token after every reinstall, restore, or app launch.
- Treat a storage read failure as signed out.
- Exclude secrets from backups when restoration cannot safely recreate their keys.
- Make sign-out delete refresh tokens and clear in-memory authentication state.
- Keep unrelated secrets out of a broad
deleteAll()call. - Never use secure storage as a backup database for large or replaceable data.
Exercise
Why should an app treat an unreadable refresh token as signed out instead of retrying the read?
Show answer
The encrypted value may have been restored without its original encryption key. Retrying will not repair the mismatch, so the app should delete the stale value and require authentication again.