You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

/// Data model representing a user session.
///
/// This model stores user identification and authentication information
/// for session management and data isolation.
class User {
/// Unique identifier for the user.
final String id;
/// Display name or username for the user.
final String username;
/// Optional authentication token or session token.
final String? token;
/// Timestamp when the session was created (milliseconds since epoch).
final int createdAt;
/// Creates a [User] instance.
///
/// [id] - Unique identifier for the user.
/// [username] - Display name or username.
/// [token] - Optional authentication token.
/// [createdAt] - Session creation timestamp (defaults to current time).
User({
required this.id,
required this.username,
this.token,
int? createdAt,
}) : createdAt = createdAt ?? DateTime.now().millisecondsSinceEpoch;
/// Creates a [User] from a Map (e.g., from database or JSON).
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['id'] as String,
username: map['username'] as String,
token: map['token'] as String?,
createdAt: map['created_at'] as int?,
);
}
/// Converts the [User] to a Map for storage.
Map<String, dynamic> toMap() {
return {
'id': id,
'username': username,
'token': token,
'created_at': createdAt,
};
}
/// Creates a copy of this [User] with updated fields.
User copyWith({
String? id,
String? username,
String? token,
int? createdAt,
}) {
return User(
id: id ?? this.id,
username: username ?? this.username,
token: token ?? this.token,
createdAt: createdAt ?? this.createdAt,
);
}
@override
String toString() {
return 'User(id: $id, username: $username, createdAt: $createdAt)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is User && other.id == id;
}
@override
int get hashCode => id.hashCode;
}

Powered by TurnKey Linux.