Skip to content

Commit

Permalink
Fix user id extraction in dart client.
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatz committed Nov 12, 2024
1 parent b647a14 commit b65e54a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 15 additions & 2 deletions client/trailbase-dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class User {
});

User.fromJson(Map<String, dynamic> json)
: id = json['id'],
: id = json['sub'],
email = json['email'];

@override
Expand All @@ -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})';
}
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 15 additions & 2 deletions client/trailbase-dart/test/trailbase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b65e54a

Please sign in to comment.