diff --git a/auth0_flutter_platform_interface/lib/src/credentials.dart b/auth0_flutter_platform_interface/lib/src/credentials.dart index 156bd870..b187e931 100644 --- a/auth0_flutter_platform_interface/lib/src/credentials.dart +++ b/auth0_flutter_platform_interface/lib/src/credentials.dart @@ -76,6 +76,7 @@ class Credentials { 'refreshToken': refreshToken, 'expiresAt': expiresAt.toUtc().toIso8601String(), 'scopes': scopes.toList(), + 'userProfile': user.toMap(), 'tokenType': tokenType, }; } diff --git a/auth0_flutter_platform_interface/lib/src/user_profile.dart b/auth0_flutter_platform_interface/lib/src/user_profile.dart index 5f5ae4fb..4e9dd669 100644 --- a/auth0_flutter_platform_interface/lib/src/user_profile.dart +++ b/auth0_flutter_platform_interface/lib/src/user_profile.dart @@ -163,4 +163,28 @@ class UserProfile { result['custom_claims'] as Map) : null, ); + + Map 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, + }; } diff --git a/auth0_flutter_platform_interface/test/credentials_test.dart b/auth0_flutter_platform_interface/test/credentials_test.dart index c9a63930..8de39bdc 100644 --- a/auth0_flutter_platform_interface/test/credentials_test.dart +++ b/auth0_flutter_platform_interface/test/credentials_test.dart @@ -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); + }); }); }