Fix user id extraction in dart client.

This commit is contained in:
Sebastian Jeltsch
2024-11-12 13:33:52 +01:00
parent b647a148fa
commit b65e54a644
2 changed files with 30 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ class User {
});
User.fromJson(Map<String, dynamic> json)
: id = json['id'],
: id = json['sub'],
email = json['email'];
@override
@@ -40,6 +40,19 @@ class Tokens {
'csrf_token': csrf,
};
bool get valid => JwtDecoder.decode(auth).isNotEmpty;
@override
bool operator ==(Object other) {
return other is Tokens &&
auth == other.auth &&
refresh == other.refresh &&
csrf == other.csrf;
}
@override
int get hashCode => Object.hash(auth, refresh, csrf);
@override
String toString() => 'Tokens(${auth}, ${refresh}, ${csrf})';
}
@@ -352,7 +365,7 @@ class Client {
User? user() {
final authToken = tokens()?.auth;
if (authToken != null) {
return User.fromJson(JwtDecoder.decode(authToken)['user']);
return User.fromJson(JwtDecoder.decode(authToken));
}
return null;
}

View File

@@ -79,13 +79,26 @@ Future<void> main() async {
final oldTokens = client.tokens();
expect(oldTokens, isNotNull);
expect(oldTokens!.valid, isTrue);
final user = client.user()!;
expect(user.id, isNot(equals('')));
expect(user.email, equals('admin@localhost'));
await client.logout();
expect(client.tokens(), isNull);
// We need to wait a little to push the expiry time in seconds to avoid just getting the same token minted again.
await Future.delayed(Duration(milliseconds: 1500));
final newTokens = await client.login('admin@localhost', 'secret');
expect(newTokens, isNotNull);
expect(newTokens.valid, isTrue);
expect(newTokens, isNot(equals(oldTokens)));
await client.refreshAuthToken();
final newTokens = client.tokens();
expect(newTokens, isNot(equals(oldTokens!.auth)));
expect(newTokens, equals(client.tokens()));
});
test('records', () async {