Skip to content

Commit a44b8db

Browse files
authored
Set user_hash, user_id, email in intercomSettings (#483)
1 parent 9e65d58 commit a44b8db

File tree

8 files changed

+71
-10
lines changed

8 files changed

+71
-10
lines changed

intercom_flutter/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Bump Intercom iOS SDK version to 18.2.0
77
* Added API `isUserLoggedIn`.
88
* Added API `fetchLoggedInUserAttributes`.
9+
* Fixed [#479](https://github.com/v3rm0n/intercom_flutter/issues/479).
10+
* Fixed [#481](https://github.com/v3rm0n/intercom_flutter/issues/481).
911

1012
## 9.1.1
1113

intercom_flutter/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ But you can pre-define some Intercom settings, if you want (optional).
149149
- [ ] handlePush
150150
- [ ] displayCarousel
151151
- [ ] displayHelpCenterCollections
152-
- [ ] isUserLoggedIn
153-
- [ ] fetchLoggedInUserAttributes
154152

155153
## Using Intercom keys with `--dart-define`
156154

intercom_flutter/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
flutter_web_plugins:
1111
sdk: flutter
1212
intercom_flutter_platform_interface: ^2.0.2
13-
intercom_flutter_web: ^1.1.4
13+
intercom_flutter_web: ^1.1.5
1414

1515
dev_dependencies:
1616
flutter_test:

intercom_flutter_web/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.1.5
4+
5+
* Set user_hash, user_id, email in intercomSettings.
6+
* Implemented methods `isUserLoggedIn` and `fetchLoggedInUserAttributes`.
7+
38
## 1.1.4
49

510
* Updated dependency `intercom_flutter_platform_interface: ^2.0.2`.

intercom_flutter_web/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ But you can pre-define some Intercom settings, if you want (optional).
2626
- handlePush
2727
- displayCarousel
2828
- displayHelpCenterCollections
29-
- isUserLoggedIn
30-
- fetchLoggedInUserAttributes
3129

3230
[1]: ../intercom_flutter
3331

intercom_flutter_web/example/integration_test/intercom_flutter_web_test.dart

+8
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,13 @@ void main() {
138138
testWidgets('displayHome', (WidgetTester _) async {
139139
expect(plugin.displayHome(), completes);
140140
});
141+
142+
testWidgets('isUserLoggedIn', (WidgetTester _) async {
143+
expect(plugin.isUserLoggedIn(), completes);
144+
});
145+
146+
testWidgets('fetchLoggedInUserAttributes', (WidgetTester _) async {
147+
expect(plugin.fetchLoggedInUserAttributes(), completes);
148+
});
141149
});
142150
}

intercom_flutter_web/lib/intercom_flutter_web.dart

+54-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
7373
globalContext.callMethod(
7474
'Intercom'.toJS,
7575
'update'.toJS,
76-
{'user_hash': userHash}.jsify(),
76+
updateIntercomSettings('user_hash', userHash).jsify(),
7777
);
7878
print("user hash added");
7979
}
@@ -93,7 +93,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
9393
globalContext.callMethod(
9494
'Intercom'.toJS,
9595
'update'.toJS,
96-
{'user_id': userId}.jsify(),
96+
updateIntercomSettings('user_id', userId).jsify(),
9797
);
9898
// send the success callback only as web does not support the statusCallback.
9999
statusCallback?.onSuccess?.call();
@@ -102,7 +102,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
102102
globalContext.callMethod(
103103
'Intercom'.toJS,
104104
'update'.toJS,
105-
{'email': email}.jsify(),
105+
updateIntercomSettings('email', email).jsify(),
106106
);
107107
// send the success callback only as web does not support the statusCallback.
108108
statusCallback?.onSuccess?.call();
@@ -120,7 +120,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
120120
globalContext.callMethod(
121121
'Intercom'.toJS,
122122
'update'.toJS,
123-
{'user_id': userId}.jsify(),
123+
updateIntercomSettings('user_id', userId).jsify(),
124124
);
125125
// send the success callback only as web does not support the statusCallback.
126126
statusCallback?.onSuccess?.call();
@@ -213,6 +213,11 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
213213

214214
@override
215215
Future<void> logout() async {
216+
// shutdown will effectively clear out any user data that you have been passing through the JS API.
217+
// but not from intercomSettings
218+
// so manually clear some intercom settings
219+
removeIntercomSettings(['user_hash', 'user_id', 'email']);
220+
// shutdown
216221
globalContext.callMethod('Intercom'.toJS, 'shutdown'.toJS);
217222
print("logout");
218223
}
@@ -292,6 +297,35 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
292297
print("Launched Home space");
293298
}
294299

300+
@override
301+
Future<bool> isUserLoggedIn() async {
302+
// There is no direct JS API available
303+
// Here we check if intercomSettings has user_id or email then user is
304+
// logged in
305+
var settings = getIntercomSettings();
306+
var user_id = settings['user_id'] as String? ?? "";
307+
var email = settings['email'] as String? ?? "";
308+
309+
return user_id.isNotEmpty || email.isNotEmpty;
310+
}
311+
312+
@override
313+
Future<Map<String, dynamic>> fetchLoggedInUserAttributes() async {
314+
// There is no direct JS API available
315+
// Just return the user_id or email from intercomSettings
316+
var settings = getIntercomSettings();
317+
var user_id = settings['user_id'] as String? ?? "";
318+
var email = settings['email'] as String? ?? "";
319+
320+
if (user_id.isNotEmpty) {
321+
return {'user_id': user_id};
322+
} else if (email.isNotEmpty) {
323+
return {'email': email};
324+
}
325+
326+
return {};
327+
}
328+
295329
/// get the [window.intercomSettings]
296330
Map<dynamic, dynamic> getIntercomSettings() {
297331
if (globalContext.hasProperty('intercomSettings'.toJS).toDart) {
@@ -316,4 +350,20 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
316350

317351
return intercomSettings;
318352
}
353+
354+
/// Remove properties from [window.intercomSettings]
355+
Map<dynamic, dynamic> removeIntercomSettings(List<String> keys) {
356+
var intercomSettings = getIntercomSettings();
357+
358+
// remove the keys
359+
for (var key in keys) {
360+
intercomSettings.remove(key);
361+
}
362+
363+
// Update the [window.intercomSettings]
364+
globalContext.setProperty(
365+
"intercomSettings".toJS, intercomSettings.jsify());
366+
367+
return intercomSettings;
368+
}
319369
}

intercom_flutter_web/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: intercom_flutter_web
22
description: Web platform implementation of intercom_flutter
3-
version: 1.1.4
3+
version: 1.1.5
44
homepage: https://github.com/v3rm0n/intercom_flutter
55

66
flutter:

0 commit comments

Comments
 (0)