add auth, format

This commit is contained in:
Phuoc Nguyen
2025-11-07 11:52:06 +07:00
parent 24a8508fce
commit 3803bd26e0
173 changed files with 8505 additions and 7116 deletions

View File

@@ -0,0 +1,86 @@
/// Authentication Session Model
///
/// Models for API authentication response structure.
/// Matches the ERPNext login API response format.
library;
import 'package:freezed_annotation/freezed_annotation.dart';
part 'auth_session_model.freezed.dart';
part 'auth_session_model.g.dart';
/// App Information
///
/// Represents an available app in the system.
@freezed
sealed class AppInfo with _$AppInfo {
const factory AppInfo({
@JsonKey(name: 'app_title') required String appTitle,
@JsonKey(name: 'app_endpoint') required String appEndpoint,
@JsonKey(name: 'app_logo') required String appLogo,
}) = _AppInfo;
factory AppInfo.fromJson(Map<String, dynamic> json) =>
_$AppInfoFromJson(json);
}
/// Login Response Message
///
/// Contains the core authentication data from login response.
@freezed
sealed class LoginMessage with _$LoginMessage {
const factory LoginMessage({
required bool success,
required String message,
required String sid,
@JsonKey(name: 'csrf_token') required String csrfToken,
@Default([]) List<AppInfo> apps,
}) = _LoginMessage;
factory LoginMessage.fromJson(Map<String, dynamic> json) =>
_$LoginMessageFromJson(json);
}
/// Authentication Session Response
///
/// Complete authentication response from ERPNext login API.
@freezed
sealed class AuthSessionResponse with _$AuthSessionResponse {
const factory AuthSessionResponse({
@JsonKey(name: 'session_expired') required int sessionExpired,
required LoginMessage message,
@JsonKey(name: 'home_page') required String homePage,
@JsonKey(name: 'full_name') required String fullName,
}) = _AuthSessionResponse;
factory AuthSessionResponse.fromJson(Map<String, dynamic> json) =>
_$AuthSessionResponseFromJson(json);
}
/// Session Storage Model
///
/// Simplified model for storing session data in Hive.
@freezed
sealed class SessionData with _$SessionData {
const factory SessionData({
required String sid,
required String csrfToken,
required String fullName,
required DateTime createdAt,
List<AppInfo>? apps,
}) = _SessionData;
factory SessionData.fromJson(Map<String, dynamic> json) =>
_$SessionDataFromJson(json);
/// Create from API response
factory SessionData.fromAuthResponse(AuthSessionResponse response) {
return SessionData(
sid: response.message.sid,
csrfToken: response.message.csrfToken,
fullName: response.fullName,
createdAt: DateTime.now(),
apps: response.message.apps,
);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,131 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'auth_session_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_AppInfo _$AppInfoFromJson(Map<String, dynamic> json) => $checkedCreate(
'_AppInfo',
json,
($checkedConvert) {
final val = _AppInfo(
appTitle: $checkedConvert('app_title', (v) => v as String),
appEndpoint: $checkedConvert('app_endpoint', (v) => v as String),
appLogo: $checkedConvert('app_logo', (v) => v as String),
);
return val;
},
fieldKeyMap: const {
'appTitle': 'app_title',
'appEndpoint': 'app_endpoint',
'appLogo': 'app_logo',
},
);
Map<String, dynamic> _$AppInfoToJson(_AppInfo instance) => <String, dynamic>{
'app_title': instance.appTitle,
'app_endpoint': instance.appEndpoint,
'app_logo': instance.appLogo,
};
_LoginMessage _$LoginMessageFromJson(Map<String, dynamic> json) =>
$checkedCreate('_LoginMessage', json, ($checkedConvert) {
final val = _LoginMessage(
success: $checkedConvert('success', (v) => v as bool),
message: $checkedConvert('message', (v) => v as String),
sid: $checkedConvert('sid', (v) => v as String),
csrfToken: $checkedConvert('csrf_token', (v) => v as String),
apps: $checkedConvert(
'apps',
(v) =>
(v as List<dynamic>?)
?.map((e) => AppInfo.fromJson(e as Map<String, dynamic>))
.toList() ??
const [],
),
);
return val;
}, fieldKeyMap: const {'csrfToken': 'csrf_token'});
Map<String, dynamic> _$LoginMessageToJson(_LoginMessage instance) =>
<String, dynamic>{
'success': instance.success,
'message': instance.message,
'sid': instance.sid,
'csrf_token': instance.csrfToken,
'apps': instance.apps.map((e) => e.toJson()).toList(),
};
_AuthSessionResponse _$AuthSessionResponseFromJson(Map<String, dynamic> json) =>
$checkedCreate(
'_AuthSessionResponse',
json,
($checkedConvert) {
final val = _AuthSessionResponse(
sessionExpired: $checkedConvert(
'session_expired',
(v) => (v as num).toInt(),
),
message: $checkedConvert(
'message',
(v) => LoginMessage.fromJson(v as Map<String, dynamic>),
),
homePage: $checkedConvert('home_page', (v) => v as String),
fullName: $checkedConvert('full_name', (v) => v as String),
);
return val;
},
fieldKeyMap: const {
'sessionExpired': 'session_expired',
'homePage': 'home_page',
'fullName': 'full_name',
},
);
Map<String, dynamic> _$AuthSessionResponseToJson(
_AuthSessionResponse instance,
) => <String, dynamic>{
'session_expired': instance.sessionExpired,
'message': instance.message.toJson(),
'home_page': instance.homePage,
'full_name': instance.fullName,
};
_SessionData _$SessionDataFromJson(Map<String, dynamic> json) => $checkedCreate(
'_SessionData',
json,
($checkedConvert) {
final val = _SessionData(
sid: $checkedConvert('sid', (v) => v as String),
csrfToken: $checkedConvert('csrf_token', (v) => v as String),
fullName: $checkedConvert('full_name', (v) => v as String),
createdAt: $checkedConvert(
'created_at',
(v) => DateTime.parse(v as String),
),
apps: $checkedConvert(
'apps',
(v) => (v as List<dynamic>?)
?.map((e) => AppInfo.fromJson(e as Map<String, dynamic>))
.toList(),
),
);
return val;
},
fieldKeyMap: const {
'csrfToken': 'csrf_token',
'fullName': 'full_name',
'createdAt': 'created_at',
},
);
Map<String, dynamic> _$SessionDataToJson(_SessionData instance) =>
<String, dynamic>{
'sid': instance.sid,
'csrf_token': instance.csrfToken,
'full_name': instance.fullName,
'created_at': instance.createdAt.toIso8601String(),
'apps': ?instance.apps?.map((e) => e.toJson()).toList(),
};