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

feat(auth): 🔐 Add UserProfile serialization and include in Credentials #475

Open
wants to merge 1 commit 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,
};
}
25 changes: 25 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,29 @@ 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?.toIso8601String(),
'custom_claims': customClaims,
};
}
16 changes: 16 additions & 0 deletions auth0_flutter_platform_interface/test/credentials_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ void main() {
});

expect(credentials.expiresAt.isUtc, true);
expect(
credentials.user.toMap(),
UserProfile.fromMap(
{
'sub': '123',
'name': 'John Doe',
},
).toMap(),
);
});

test('Credentials throws when expiresAt Locale set to ar', () async {
Expand Down Expand Up @@ -74,6 +83,13 @@ void main() {
tokenType: 'Bearer');

expect(credentials.toMap()['expiresAt'], '2023-11-01T22:16:35.760Z');
expect(
credentials.toMap()['userProfile'],
{
'sub': '123',
'name': 'John Doe',
},
);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ void main() {
expect(verificationResult.arguments['credentials']['scopes'], ['a']);
expect(
verificationResult.arguments['credentials']['tokenType'], 'Bearer');
expect(
verificationResult.arguments['credentials']['userProfile'], isNull);
expect(verificationResult.arguments['credentials']['userProfile'],
isNotNull);
});

test(
Expand Down
137 changes: 137 additions & 0 deletions auth0_flutter_platform_interface/test/user_profile_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('UserProfile', () {
test('fromMap correctly maps all properties', () {
final map = {
'sub': 'user123',
'name': 'John Doe',
'given_name': 'John',
'family_name': 'Doe',
'middle_name': 'Smith',
'nickname': 'Johnny',
'preferred_username': 'john.doe',
'profile': 'https://example.com/profile',
'picture': 'https://example.com/picture.jpg',
'website': 'https://johndoe.com',
'email': 'john@example.com',
'email_verified': true,
'gender': 'male',
'birthdate': '1990-01-01',
'zoneinfo': 'America/New_York',
'locale': 'en-US',
'phone_number': '+1234567890',
'phone_number_verified': true,
'address': {'country': 'USA', 'postal_code': '12345'},
'updated_at': '2023-01-01T00:00:00.000Z',
'custom_claims': {'role': 'admin'}
};

final userProfile = UserProfile.fromMap(map);

expect(userProfile.sub, 'user123');
expect(userProfile.name, 'John Doe');
expect(userProfile.givenName, 'John');
expect(userProfile.familyName, 'Doe');
expect(userProfile.middleName, 'Smith');
expect(userProfile.nickname, 'Johnny');
expect(userProfile.preferredUsername, 'john.doe');
expect(userProfile.profileUrl, Uri.parse('https://example.com/profile'));
expect(
userProfile.pictureUrl, Uri.parse('https://example.com/picture.jpg'));
expect(userProfile.websiteUrl, Uri.parse('https://johndoe.com'));
expect(userProfile.email, 'john@example.com');
expect(userProfile.isEmailVerified, true);
expect(userProfile.gender, 'male');
expect(userProfile.birthdate, '1990-01-01');
expect(userProfile.zoneinfo, 'America/New_York');
expect(userProfile.locale, 'en-US');
expect(userProfile.phoneNumber, '+1234567890');
expect(userProfile.isPhoneNumberVerified, true);
expect(userProfile.address, {'country': 'USA', 'postal_code': '12345'});
expect(userProfile.updatedAt, DateTime.parse('2023-01-01T00:00:00.000Z'));
expect(userProfile.customClaims, {'role': 'admin'});
});

test('fromMap handles missing optional properties', () {
final map = {
'sub': 'user123',
'email': 'john@example.com',
};

final userProfile = UserProfile.fromMap(map);

expect(userProfile.sub, 'user123');
expect(userProfile.email, 'john@example.com');
expect(userProfile.name, isNull);
expect(userProfile.givenName, isNull);
expect(userProfile.familyName, isNull);
expect(userProfile.middleName, isNull);
expect(userProfile.nickname, isNull);
expect(userProfile.preferredUsername, isNull);
expect(userProfile.profileUrl, isNull);
expect(userProfile.pictureUrl, isNull);
expect(userProfile.websiteUrl, isNull);
expect(userProfile.isEmailVerified, isNull);
expect(userProfile.gender, isNull);
expect(userProfile.birthdate, isNull);
expect(userProfile.zoneinfo, isNull);
expect(userProfile.locale, isNull);
expect(userProfile.phoneNumber, isNull);
expect(userProfile.isPhoneNumberVerified, isNull);
expect(userProfile.address, isNull);
expect(userProfile.updatedAt, isNull);
expect(userProfile.customClaims, isNull);
});

test('toMap correctly converts all properties', () {
final userProfile = UserProfile(
sub: 'user123',
name: 'John Doe',
givenName: 'John',
familyName: 'Doe',
middleName: 'Smith',
nickname: 'Johnny',
preferredUsername: 'john.doe',
profileUrl: Uri.parse('https://example.com/profile'),
pictureUrl: Uri.parse('https://example.com/picture.jpg'),
websiteUrl: Uri.parse('https://johndoe.com'),
email: 'john@example.com',
isEmailVerified: true,
gender: 'male',
birthdate: '1990-01-01',
zoneinfo: 'America/New_York',
locale: 'en-US',
phoneNumber: '+1234567890',
isPhoneNumberVerified: true,
address: {'country': 'USA', 'postal_code': '12345'},
updatedAt: DateTime.parse('2023-01-01T00:00:00.000Z'),
customClaims: {'role': 'admin'});

final map = userProfile.toMap();

expect(map['sub'], 'user123');
expect(map['name'], 'John Doe');
expect(map['given_name'], 'John');
expect(map['family_name'], 'Doe');
expect(map['middle_name'], 'Smith');
expect(map['nickname'], 'Johnny');
expect(map['preferred_username'], 'john.doe');
expect(map['profile'], 'https://example.com/profile');
expect(map['picture'], 'https://example.com/picture.jpg');
expect(map['website'], 'https://johndoe.com');
expect(map['email'], 'john@example.com');
expect(map['email_verified'], true);
expect(map['gender'], 'male');
expect(map['birthdate'], '1990-01-01');
expect(map['zoneinfo'], 'America/New_York');
expect(map['locale'], 'en-US');
expect(map['phone_number'], '+1234567890');
expect(map['phone_number_verified'], true);
expect(map['address'], {'country': 'USA', 'postal_code': '12345'});
expect(map['updated_at'], '2023-01-01T00:00:00.000Z');
expect(map['custom_claims'], {'role': 'admin'});
});
});
}