Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserProfile.toMap and unit test #433

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Credentials {
'refreshToken': refreshToken,
'expiresAt': expiresAt.toUtc().toIso8601String(),
'scopes': scopes.toList(),
'userProfile': user.toMap(),
'tokenType': tokenType,
};
}
24 changes: 24 additions & 0 deletions auth0_flutter_platform_interface/lib/src/user_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ class UserProfile {
result['custom_claims'] as Map<dynamic, dynamic>)
: null,
);

Map<String, dynamic> toMap() => {
'sub': sub,
'name': name,
'given_name': givenName,
'family_name': familyName,
'middle_name': middleName,
'nickname': nickname,
'preferred_username': preferredUsername,
'profile': profileUrl?.toString(),
'picture': pictureUrl?.toString(),
'website': websiteUrl?.toString(),
'email': email,
'email_verified': isEmailVerified,
'gender': gender,
'birthdate': birthdate,
'zoneinfo': zoneinfo,
'locale': locale,
'phone_number': phoneNumber,
'phone_number_verified': isPhoneNumberVerified,
'address': address,
'updated_at': updatedAt?.toUtc().toIso8601String(),
'custom_claims': customClaims,
};
}
51 changes: 51 additions & 0 deletions auth0_flutter_platform_interface/test/credentials_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,57 @@ void main() {

expect(credentials.toMap()['expiresAt'], '2023-11-01T22:16:35.760Z');
});

test('converting to a map and back again populates the user property', () {
final dateTime = DateTime.utc(2023, 11, 2);
final updatedAt = DateTime.utc(2024, 10, 3);
const userName = 'User name';
const customClaimValue = 1;
const exampleUrl =
'https://store.google.com/ca/product/pixel_tablet?hl=en-GB';

final credentials = Credentials(
accessToken: 'accessToken',
idToken: 'idToken',
refreshToken: 'refreshToken',
expiresAt: dateTime,
scopes: {'a'},
user: UserProfile(
sub: 'sub',
name: userName,
givenName: 'givenName',
familyName: 'familyName',
middleName: 'middleName',
nickname: 'nickname',
preferredUsername: 'preferredUsername',
profileUrl: Uri.parse(exampleUrl),
pictureUrl: Uri.parse(exampleUrl),
websiteUrl: Uri.parse(exampleUrl),
email: 'email',
isEmailVerified: true,
gender: 'gender',
birthdate: 'birthdate',
zoneinfo: 'zoneinfo',
locale: 'locale',
phoneNumber: 'phoneNumber',
isPhoneNumberVerified: true,
address: {
'line_1': '123 Fox Lane',
},
updatedAt: updatedAt,
customClaims: {
'my_claim': customClaimValue,
},
),
tokenType: 'Bearer',
);

final result = Credentials.fromMap(credentials.toMap());

expect(result.user.name, userName);
expect(result.user.customClaims?['my_claim'], customClaimValue);
expect(result.user.websiteUrl?.toString(), exampleUrl);
});
});
}

Expand Down
Loading