refactor: move identifier type detection into AuthenticationIdentifier

This commit is contained in:
Lukas Lihotzki 2021-05-05 12:08:03 +02:00
parent 2fdf3e8284
commit 846c96e90d
2 changed files with 20 additions and 17 deletions

View File

@ -21,6 +21,11 @@
* SOFTWARE.
*/
import 'authentication_types.dart';
import 'authentication_user_identifier.dart';
import 'authentication_phone_identifier.dart';
import 'authentication_third_party_identifier.dart';
class AuthenticationIdentifier {
String type;
@ -29,6 +34,19 @@ class AuthenticationIdentifier {
AuthenticationIdentifier.fromJson(Map<String, dynamic> json)
: type = json['type'];
factory AuthenticationIdentifier.subFromJson(Map<String, dynamic> json) {
switch (json['type']) {
case AuthenticationIdentifierTypes.userId:
return AuthenticationUserIdentifier.fromJson(json);
case AuthenticationIdentifierTypes.phone:
return AuthenticationPhoneIdentifier.fromJson(json);
case AuthenticationIdentifierTypes.thirdParty:
return AuthenticationThirdPartyIdentifier.fromJson(json);
default:
return AuthenticationIdentifier.fromJson(json);
}
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['type'] = type;

View File

@ -25,8 +25,6 @@ import 'authentication_user_identifier.dart';
import 'authentication_data.dart';
import 'authentication_identifier.dart';
import 'authentication_phone_identifier.dart';
import 'authentication_third_party_identifier.dart';
import 'authentication_types.dart';
class AuthenticationPassword extends AuthenticationData {
@ -47,21 +45,8 @@ class AuthenticationPassword extends AuthenticationData {
AuthenticationPassword.fromJson(Map<String, dynamic> json)
: user = json['user'],
password = json['password'],
identifier = AuthenticationIdentifier.fromJson(json['identifier']),
super.fromJson(json) {
switch (identifier.type) {
case AuthenticationIdentifierTypes.userId:
identifier = AuthenticationUserIdentifier.fromJson(json['identifier']);
break;
case AuthenticationIdentifierTypes.phone:
identifier = AuthenticationPhoneIdentifier.fromJson(json['identifier']);
break;
case AuthenticationIdentifierTypes.thirdParty:
identifier =
AuthenticationThirdPartyIdentifier.fromJson(json['identifier']);
break;
}
}
identifier = AuthenticationIdentifier.subFromJson(json['identifier']),
super.fromJson(json);
@override
Map<String, dynamic> toJson() {