Concepts

Storage

Where sessions are kept, how they survive restarts and failures, and how to plug your own storage.

The options

StoragePackageUse it for
SecureAuthStorageauthyra_flutterApps. Keychain on iOS and macOS, Keystore-backed encryption on Android, libsecret on Linux, DPAPI on Windows.
InMemoryAuthStorageauthyraTests and demos. Nothing survives a restart.
Your own AuthStorageA Dart CLI or server, or a platform with its own secret store.

SecureAuthStorage defaults keep tokens readable after the first unlock following a reboot, so background refreshes work while the device is locked, and never copy them to other devices through backups or iCloud Keychain.

What is stored

Each session is a JSON document under its own key, prefixed with authyra., plus one key naming the active session. Dates are written in UTC. Other entries your app keeps in the same secure storage are left alone.

Ordering guarantees

  • Storage before memory. A sign-in, sign-out or switch is applied in memory only once written. If the write fails, the operation throws AuthStorageException and the local state stays as it was. For a sign-out, the server-side revocation has already run, so the session is kept locally with tokens the server no longer accepts; call signOut again.
  • One exception: rotated tokens. They are kept in memory even if their write fails, and written again later, because the server has already consumed the previous refresh token.
  • Writes are serialized, so concurrent operations cannot interleave.

iOS keeps the keychain after uninstall

Keychain items survive an app uninstall on iOS. If a reinstalled app must start signed out, clear Authyra's entries on first launch:

final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('installed') != true) {
  await const SecureAuthStorage().deleteAll(); // Only Authyra's keys.
  await prefs.setBool('installed', true);      // Wiped on uninstall.
}
final authyra = await Authyra.initialize(storage: const SecureAuthStorage(), providers: [...]);

Writing your own

Four methods, strings only. Encryption is the implementation's job.

final class FileAuthStorage implements AuthStorage {
  @override
  Future<String?> read(String key) async { /* ... */ }

  @override
  Future<void> write(String key, String value) async { /* ... */ }

  @override
  Future<void> delete(String key) async { /* ... */ }

  /// Called once at startup to restore every session in one pass.
  @override
  Future<Map<String, String>> readAll({required String prefix}) async { /* ... */ }
}

delete of a missing key must not throw.

Copyright © 2026