session_box 1.0.0
session_box: ^1.0.0 copied to clipboard
A Flutter package that allows you store your currently logged in user for quick access
๐๏ธ user_session_manager #
A clean, lightweight, and testable Dart package for securely managing user session data across app restarts using SharedPreferences or flutter_secure_storage.
Designed with SOLID principles and clean architecture, it supports both primitive types (like String, int) and custom objects using JSON serialization.
This is just functionality that I wrote for my own app. I felt like that the functionality should have been provided by an existing package, but there was none.
โจ Features #
- Choose between secure or plain storage (with
encrypt: true) - Persist any data type (primitives or complex models)
- Blazing fast access to session state
- Fully testable with in-memory mock repository
- Easy to integrate into any Dart or Flutter app
- No DB dependency required for login state
๐ฆ Installation #
Add this to your pubspec.yaml:
dependencies:
(coming soon)
Then run:
flutter pub get
โจ Will be available on pub.dev soon!
๐ Getting Started #
Primitive Example (int - userId) #
final session = await UserSessionManager.create<int>(encrypt: false);
await session.login(42); // Save user ID
final userId = await session.getUser(); // 42
final isLoggedIn = await session.isLoggedIn(); // true
await session.logout();
Custom Object Example #
class User {
final int id;
final String name;
User(this.id, this.name);
Map<String, dynamic> toJson() => {'id': id, 'name': name};
factory User.fromJson(Map<String, dynamic> json) =>
User(json['id'], json['name']);
factory User.fromJson(Map<String, dynamic> json) => User(
json['id'],
json['name'],
);
}
final session = await UserSessionManager.create<User>(
encrypt: true,
toJson: (u) => u.toJson(),
fromJson: (json) => User.fromJson(json),
);
await session.login(User(1, 'Alice'));
final user = await session.getUser(); // User instance
๐งช Testing with In-Memory Storage #
For testing without platform dependencies:
final repo = InMemorySessionRepository<User>();
final service = UserSessionService.forTesting(repo);
final session = UserSessionManager.forTesting(service);
API #
UserSessionManager<T> #
| Method | Description |
|---|---|
login(T user) |
Saves the session |
logout() |
Clears the session |
getUser() |
Retrieves the stored object or null |
isLoggedIn() |
Returns true if a user exists |
When to Use #
| Use Case | Use this package? |
|---|---|
| Fast session check at startup | โ Yes |
| Complex profile management | โ Use your DB |
| Persisting lightweight identity | โ Yes |
| Storing auth tokens securely | โ
Yes (encrypt) |
| Testing login logic without DB | โ Yes |
๐ก๏ธ Security #
- When
encrypt: true, session data is stored viaflutter_secure_storage - โ Do not store sensitive data like raw passwords
๐ Folder Structure #
lib/
โโโ user_session_manager.dart # Public API
โโโ src/
โโโ api/
โโโ application/
โโโ domain/
โโโ data/
๐ License #
MIT License. See LICENSE for details.