session_box 1.0.0 copy "session_box: ^1.0.0" to clipboard
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 via flutter_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.

1
likes
0
points
192
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that allows you store your currently logged in user for quick access

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_secure_storage, shared_preferences

More

Packages that depend on session_box