mirror of
https://github.com/trailbaseio/trailbase.git
synced 2026-02-22 10:48:45 -06:00
Fix user id extraction in dart client.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user