OpenContacts/lib/models/authentication_data.dart

50 lines
1.3 KiB
Dart
Raw Permalink Normal View History

2023-04-29 13:18:46 -04:00
class AuthenticationData {
static const _unauthenticated = AuthenticationData(
userId: "",
token: "",
secretMachineIdHash: "",
isAuthenticated: false,
uid: "",
);
2023-04-29 13:18:46 -04:00
final String userId;
final String token;
2023-09-29 03:51:46 -04:00
final String secretMachineIdHash;
2023-04-29 13:18:46 -04:00
final bool isAuthenticated;
final String uid;
2023-04-29 13:18:46 -04:00
const AuthenticationData({
required this.userId,
required this.token,
required this.secretMachineIdHash,
required this.isAuthenticated,
required this.uid,
2023-04-29 13:18:46 -04:00
});
2023-04-29 15:26:12 -04:00
factory AuthenticationData.fromMap(Map map) {
2023-09-29 03:51:46 -04:00
map = map["entity"];
2023-04-29 15:26:12 -04:00
final userId = map["userId"];
final token = map["token"];
2023-09-29 03:51:46 -04:00
final machineId = map["secretMachineIdHash"];
final uid = map["uid"];
if (userId == null || token == null || machineId == null || uid == null) {
2023-04-29 13:18:46 -04:00
return _unauthenticated;
}
return AuthenticationData(userId: userId, token: token, secretMachineIdHash: machineId, isAuthenticated: true, uid: uid);
2023-04-29 13:18:46 -04:00
}
factory AuthenticationData.unauthenticated() => _unauthenticated;
Map<String, String> get authorizationHeader => {
"Authorization": "res $userId:$token",
};
Map<String, dynamic> toMap() {
return {
"userId": userId,
"token": token,
2023-09-29 03:51:46 -04:00
"secretMachineId": secretMachineIdHash,
"uid": uid,
};
}
}