9d1a37e248
Build and Push Docker Images / detect-changes (push) Successful in 10s
Build and Push Docker Images / build-auth-api (push) Successful in 58s
Build and Push Docker Images / build-employee-api (push) Successful in 58s
Build and Push Docker Images / build-transit-api (push) Successful in 59s
Build and Push Docker Images / build-error-log-api (push) Successful in 1m8s
Build and Push Docker Images / build-admin-ui (push) Successful in 1m14s
Build and Push Docker Images / deploy-test-node (push) Successful in 5s
86 lines
2.3 KiB
Dart
86 lines
2.3 KiB
Dart
/// User model
|
|
class User {
|
|
final int id;
|
|
final int userCode;
|
|
final String? job;
|
|
|
|
User({
|
|
required this.id,
|
|
required this.userCode,
|
|
this.job,
|
|
});
|
|
|
|
factory User.fromJson(Map<String, dynamic> json) {
|
|
// userCode may be provided as 'userCode' or 'usercode' and may be int or string
|
|
final dynamic rawUserCode = json['userCode'] ?? json['usercode'] ?? json['user_code'];
|
|
int parsedUserCode;
|
|
if (rawUserCode is int) {
|
|
parsedUserCode = rawUserCode;
|
|
} else if (rawUserCode is String) {
|
|
parsedUserCode = int.tryParse(rawUserCode) ?? (throw FormatException('Invalid userCode'));
|
|
} else {
|
|
// If the backend doesn't return a userCode, treat as unknown (-1)
|
|
parsedUserCode = -1;
|
|
}
|
|
|
|
return User(
|
|
id: json['id'] as int,
|
|
userCode: parsedUserCode,
|
|
job: json['job'] as String?,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'userCode': userCode,
|
|
'job': job,
|
|
};
|
|
}
|
|
}
|
|
|
|
/// Login response model
|
|
class AuthResponse {
|
|
final String token;
|
|
final User? user;
|
|
|
|
AuthResponse({
|
|
required this.token,
|
|
this.user,
|
|
});
|
|
|
|
factory AuthResponse.fromJson(Map<String, dynamic> json) {
|
|
final dynamic rawToken = json['token'] ?? json['Token'] ?? json['authToken'];
|
|
final token = rawToken?.toString();
|
|
if (token == null || token.isEmpty) throw FormatException('Missing token in auth response');
|
|
|
|
// Determine if a user object exists and appears valid
|
|
Map<String, dynamic>? userMap;
|
|
if (json.containsKey('user') && json['user'] is Map<String, dynamic>) {
|
|
userMap = json['user'] as Map<String, dynamic>;
|
|
} else {
|
|
// maybe user fields are top-level (id, userCode, job)
|
|
final hasUserFields = json.containsKey('id') || json.containsKey('userCode') || json.containsKey('usercode') || json.containsKey('user_code');
|
|
if (hasUserFields) {
|
|
// treat whole json as user map
|
|
userMap = json.cast<String, dynamic>();
|
|
}
|
|
}
|
|
|
|
User? parsedUser;
|
|
if (userMap != null) {
|
|
try {
|
|
parsedUser = User.fromJson(userMap);
|
|
} catch (e) {
|
|
// If parsing user fails, ignore user but keep token
|
|
parsedUser = null;
|
|
}
|
|
}
|
|
|
|
return AuthResponse(
|
|
token: token,
|
|
user: parsedUser,
|
|
);
|
|
}
|
|
}
|