Skip to content

Commit 5a69925

Browse files
authored
Merge pull request #79 from intercom/santhosh/handled_non_string_userid
Handled when userID is a Number or String
2 parents 5c9eeaf + 505d1c0 commit 5a69925

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

android/src/main/java/com/intercom/reactnative/IntercomHelpers.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,23 @@ public static UserAttributes buildUserAttributes(ReadableMap readableMap) {
222222
}
223223
return builder.build();
224224
}
225+
226+
public static String getValueAsStringForKey(ReadableMap map, String key) {
227+
ReadableType type = map.getType(key);
228+
String value = "";
229+
switch (type) {
230+
case Number: {
231+
value = String.valueOf(map.getInt(key));
232+
break;
233+
}
234+
case String: {
235+
value = map.getString(key);
236+
break;
237+
}
238+
default: {
239+
throw new IllegalArgumentException("Value for Key: \""+key+"\" should be a String");
240+
}
241+
}
242+
return value;
243+
}
225244
}

android/src/main/java/com/intercom/reactnative/IntercomModule.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,36 @@ public void onFailure(@NonNull IntercomError intercomError) {
133133

134134
@ReactMethod
135135
public void loginUserWithUserAttributes(ReadableMap params, Promise promise) {
136-
Boolean hasEmail = params.hasKey("email") && params.getString("email").length() > 0;
137-
Boolean hasUserId = params.hasKey("userId") && params.getString("userId").length() > 0;
138-
Registration registration = null;
139-
if (hasEmail && hasUserId) {
140-
registration = new Registration().withEmail(params.getString("email")).withUserId(params.getString("userId"));
141-
} else if (hasEmail) {
142-
registration = Registration.create().withEmail(params.getString("email"));
143-
} else if (hasUserId) {
144-
registration = Registration.create().withUserId(params.getString("userId"));
145-
} else {
146-
Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email");
147-
Log.e(NAME, "You must provide userId or email");
148-
promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email");
149-
}
150-
if (registration != null) {
151-
Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() {
152-
@Override
153-
public void onSuccess() {
154-
promise.resolve(true);
155-
}
136+
Boolean hasEmail = params.hasKey("email") && IntercomHelpers.getValueAsStringForKey(params, "email").length() > 0;
137+
Boolean hasUserId = params.hasKey("userId") && IntercomHelpers.getValueAsStringForKey(params, "userId").length() > 0;
138+
Registration registration = null;
139+
String email = IntercomHelpers.getValueAsStringForKey(params, "email");
140+
String userId = IntercomHelpers.getValueAsStringForKey(params, "userId");
141+
if (hasEmail && hasUserId) {
142+
registration = new Registration().withEmail(email).withUserId(userId);
143+
} else if (hasEmail) {
144+
registration = Registration.create().withEmail(email);
145+
} else if (hasUserId) {
146+
registration = Registration.create().withUserId(userId);
147+
} else {
148+
Log.e(NAME, "loginUserWithUserAttributes called with invalid userId or email");
149+
Log.e(NAME, "You must provide userId or email");
150+
promise.reject(IntercomErrorCodes.IDENTIFIED_REGISTRATION, "Invalid userId or email");
151+
}
152+
if (registration != null) {
153+
Intercom.client().loginIdentifiedUser(registration, new IntercomStatusCallback() {
154+
@Override
155+
public void onSuccess() {
156+
promise.resolve(true);
157+
}
156158

157-
@Override
158-
public void onFailure(@NonNull IntercomError intercomError) {
159-
Log.e("ERROR", intercomError.getErrorMessage());
160-
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
161-
}
162-
});
163-
}
159+
@Override
160+
public void onFailure(@NonNull IntercomError intercomError) {
161+
Log.e("ERROR", intercomError.getErrorMessage());
162+
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
163+
}
164+
});
165+
}
164166
}
165167

166168
@ReactMethod

0 commit comments

Comments
 (0)