Skip to content

Commit

Permalink
Fix meteor user after logout (#41)
Browse files Browse the repository at this point in the history
* Set user to null after meteor.logout

* Correct the test case
  • Loading branch information
tanutapi authored Oct 8, 2021
1 parent bcb7eca commit 1176285
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/src/meteor_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class MeteorClient {
final BehaviorSubject<String?> _userIdSubject = BehaviorSubject();
late Stream<String?> _userIdStream;

final BehaviorSubject<Map<String, dynamic>> _userSubject = BehaviorSubject();
late Stream<Map<String, dynamic>> _userStream;
final BehaviorSubject<Map<String, dynamic>?> _userSubject = BehaviorSubject();
late Stream<Map<String, dynamic>?> _userStream;

String? _userId;
String? _token;
Expand All @@ -91,7 +91,10 @@ class MeteorClient {
{};
final Map<String, Stream<Map<String, dynamic>>> _collectionsStreams = {};

MeteorClient.connect({required String url, bool debug = false, userAgent = 'Dart Meteor / 2.0.3'}) {
MeteorClient.connect(
{required String url,
bool debug = false,
userAgent = 'DartMeteor/2.0.4'}) {
url = url.replaceFirst(RegExp(r'^http'), 'ws');
if (!url.endsWith('websocket')) {
url = url.replaceFirst(RegExp(r'/$'), '') + '/websocket';
Expand Down Expand Up @@ -190,6 +193,8 @@ class MeteorClient {
if (_collections['users'] != null &&
_collections['users']![userId] != null) {
_userSubject.add(_collections['users']![userId]);
} else {
_userSubject.add(null);
}
});
}
Expand Down Expand Up @@ -344,11 +349,11 @@ class MeteorClient {
// Accounts

/// Get the current user record, or null if no user is logged in. A reactive data source.
Stream<Map<String, dynamic>> user() {
Stream<Map<String, dynamic>?> user() {
return _userStream;
}

Map<String, dynamic> userCurrentValue() {
Map<String, dynamic>? userCurrentValue() {
return _userSubject.value;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_meteor
description: This library make connection between meteor backend and flutter app easily. Design to work seamlessly with StreamBuilder and FutureBuilder.
version: 2.0.3
version: 2.0.4
homepage: https://github.com/tanutapi/dart_meteor

environment:
Expand Down
4 changes: 4 additions & 0 deletions test/dart_meteor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,21 @@ void main() {
print('MeteorClientLoginResult: ' + result.toString());
print('UserID: ${meteor.userIdCurrentValue()}');
expect(meteor.userIdCurrentValue(), isNotNull);
expect(meteor.userCurrentValue(), isNotNull);
await meteor.logout();
expect(meteor.userIdCurrentValue(), isNull);
expect(meteor.userCurrentValue(), isNull);
});

test('meteor.logoutOtherClients', () async {
var result = await meteor.loginWithPassword('user1', 'password1');
print('MeteorClientLoginResult: ' + result.toString());
print('UserID: ${meteor.userIdCurrentValue()}');
expect(meteor.userIdCurrentValue(), isNotNull);
expect(meteor.userCurrentValue(), isNotNull);
await meteor.logoutOtherClients();
expect(meteor.userIdCurrentValue(), isNotNull);
expect(meteor.userCurrentValue(), isNotNull);
});
});

Expand Down

0 comments on commit 1176285

Please sign in to comment.