Skip to content

Commit 58e312c

Browse files
authored
Add setAuthTokens method for Data Connectors Authentication (#335)
1 parent 218685b commit 58e312c

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau
383383
Intercom.setApiKey("<Your iOS API Key>", forAppId: "<Your App ID>")
384384
....
385385
}
386-
386+
387387
```
388388

389389
#### iOS: Permissions
@@ -1206,6 +1206,39 @@ This should be called before any user login takes place.
12061206
12071207
---
12081208
1209+
### `Intercom.setAuthTokens(authTokens)`
1210+
1211+
Provide Intercom with your auth tokens which can be used for functionality such as Custom Actions with Data Connectors. You can provide multiple
1212+
tokens at once. To create tokens:
1213+
1. Go to Settings > Integrations > Authentication in your Intercom workspace
1214+
2. Create a new token with "User" type
1215+
3. Configure the token prefix and header as needed
1216+
1217+
Learn more: https://www.intercom.com/help/en/articles/6615543-setting-up-data-connectors-authentication
1218+
1219+
This should be called after any user login takes place.
1220+
1221+
**Note:** This is separate from `setUserJwt()` which is for Messenger Security. `setAuthTokens()` passes JWT tokens to
1222+
Data Connectors (e.g., Fin Actions), which use these in `Authorization: Bearer <token>` headers for API requests.
1223+
1224+
### Options
1225+
1226+
| Name | Type | Required | Description |
1227+
| ---------- | ------------------------------- | -------- | --------------------------------------------------- |
1228+
| authTokens | `{ [key: string]: string }` | yes | An object with token names as keys and JWT strings as values |
1229+
1230+
### Example
1231+
1232+
```typescript
1233+
Intercom.setAuthTokens({ security_token: "jwt_here" });
1234+
```
1235+
1236+
### Returns
1237+
1238+
`Promise<boolean>`
1239+
1240+
---
1241+
12091242
## Author
12101243

12111244
👤 **Intercom (https://www.intercom.com/)**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class IntercomErrorCodes {
1010
public static final String SET_LOG_LEVEL = "107";
1111
public static final String GET_UNREAD_CONVERSATION = "108";
1212
public static final String SET_USER_JWT = "109";
13+
public static final String SET_AUTH_TOKENS = "110";
1314
public static final String DISPLAY_MESSENGER = "201";
1415
public static final String DISPLAY_MESSENGER_COMPOSER = "202";
1516
public static final String DISPLAY_CONTENT = "203";

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Map;
1717

18+
import io.intercom.android.sdk.AuthToken;
1819
import io.intercom.android.sdk.Company;
1920
import io.intercom.android.sdk.Intercom;
2021
import io.intercom.android.sdk.UserAttributes;
@@ -255,4 +256,23 @@ public static WritableMap deconstructRegistration(Registration registration) {
255256
}
256257
return registrationMap;
257258
}
259+
260+
public static List<AuthToken> buildAuthTokensList(ReadableMap readableMap) {
261+
List<AuthToken> authTokens = new ArrayList<>();
262+
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
263+
264+
while (iterator.hasNextKey()) {
265+
String key = iterator.nextKey();
266+
ReadableType type = readableMap.getType(key);
267+
268+
if (type == ReadableType.String) {
269+
String value = readableMap.getString(key);
270+
if (key != null && value != null && !key.isEmpty() && !value.isEmpty()) {
271+
authTokens.add(new AuthToken(key, value));
272+
}
273+
}
274+
}
275+
276+
return authTokens;
277+
}
258278
}

android/src/newarch/IntercomModule.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,29 @@ public void setUserJwt(String jwt, Promise promise) {
603603
}
604604
}
605605

606+
@ReactMethod
607+
public void setAuthTokens(ReadableMap authTokens, Promise promise) {
608+
try {
609+
List<io.intercom.android.sdk.AuthToken> authTokensList = IntercomHelpers.buildAuthTokensList(authTokens);
610+
Intercom.client().setAuthTokens(authTokensList, new IntercomStatusCallback() {
611+
@Override
612+
public void onSuccess() {
613+
promise.resolve(true);
614+
}
615+
616+
@Override
617+
public void onFailure(@NonNull IntercomError intercomError) {
618+
Log.e("ERROR", intercomError.getErrorMessage());
619+
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
620+
}
621+
});
622+
} catch (Exception err) {
623+
Log.e(NAME, "setAuthTokens error:");
624+
Log.e(NAME, err.toString());
625+
promise.reject(IntercomErrorCodes.SET_AUTH_TOKENS, err.toString());
626+
}
627+
}
628+
606629
@ReactMethod
607630
public void setNeedsStatusBarAppearanceUpdate(Promise promise) {
608631
// iOS-only method, no-op on Android

android/src/oldarch/IntercomModule.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,29 @@ public void setUserJwt(String jwt, Promise promise) {
580580
}
581581
}
582582

583+
@ReactMethod
584+
public void setAuthTokens(ReadableMap authTokens, Promise promise) {
585+
try {
586+
List<io.intercom.android.sdk.AuthToken> authTokensList = IntercomHelpers.buildAuthTokensList(authTokens);
587+
Intercom.client().setAuthTokens(authTokensList, new IntercomStatusCallback() {
588+
@Override
589+
public void onSuccess() {
590+
promise.resolve(true);
591+
}
592+
593+
@Override
594+
public void onFailure(@NonNull IntercomError intercomError) {
595+
Log.e("ERROR", intercomError.getErrorMessage());
596+
promise.reject(String.valueOf(intercomError.getErrorCode()), intercomError.getErrorMessage());
597+
}
598+
});
599+
} catch (Exception err) {
600+
Log.e(NAME, "setAuthTokens error:");
601+
Log.e(NAME, err.toString());
602+
promise.reject(IntercomErrorCodes.SET_AUTH_TOKENS, err.toString());
603+
}
604+
}
605+
583606
public static synchronized void initialize(Application application, String apiKey, String appId) {
584607
String sdkVersion = BuildConfig.INTERCOM_VERSION_NAME;
585608
ReactNativeHeaderInterceptor.setReactNativeVersion(application.getApplicationContext(), sdkVersion);

ios/IntercomModule.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ @implementation IntercomModule
1616
NSString *LOG_EVENT = @"105";
1717
NSString *UNREAD_CONVERSATION_COUNT = @"107";
1818
NSString *SET_USER_JWT = @"109";
19+
NSString *SET_AUTH_TOKENS = @"110";
1920
NSString *SEND_TOKEN_TO_INTERCOM = @"302";
2021
NSString *FETCH_HELP_CENTER_COLLECTIONS = @"901";
2122
NSString *FETCH_HELP_CENTER_COLLECTION = @"902";
@@ -331,6 +332,22 @@ - (NSData *)dataFromHexString:(NSString *)string {
331332
}
332333
};
333334

335+
RCT_EXPORT_METHOD(setAuthTokens:(NSDictionary *)authTokens
336+
resolver:(RCTPromiseResolveBlock)resolve
337+
rejecter:(RCTPromiseRejectBlock)reject) {
338+
@try {
339+
[Intercom setAuthTokens:authTokens
340+
success:^{
341+
resolve(@(YES));
342+
}
343+
failure:^(NSError * _Nonnull error) {
344+
reject(SET_AUTH_TOKENS, @"Error in setAuthTokens", [self removeNullUnderlyingError:error]);
345+
}];
346+
} @catch (NSException *exception) {
347+
reject(SET_AUTH_TOKENS, @"Error in setAuthTokens", [self exceptionToError:exception :SET_AUTH_TOKENS :@"setAuthTokens"]);
348+
}
349+
};
350+
334351
RCT_EXPORT_METHOD(setInAppMessageVisibility:(NSString *)visibility
335352
resolver:(RCTPromiseResolveBlock)resolve
336353
rejecter:(RCTPromiseRejectBlock)reject) {

src/NativeIntercomSpec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface Spec extends TurboModule {
8484
setLogLevel(logLevel: string): Promise<boolean>;
8585
setThemeMode(themeMode: string): Promise<boolean>;
8686
setUserJwt(jwt: string): Promise<boolean>;
87+
setAuthTokens(authTokens: Object): Promise<boolean>;
8788
}
8889

8990
export default TurboModuleRegistry.getEnforcing<Spec>('IntercomModule');

src/index.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@ export type IntercomType = {
277277
*/
278278
setUserJwt(jwt: string): Promise<boolean>;
279279

280+
/**
281+
* Provide Intercom with your auth tokens which can be used for functionality
282+
* such as Custom Actions with Data Connectors. You can provide multiple tokens at once. To create tokens:
283+
* 1. Go to Settings > Integrations > Authentication in your Intercom workspace
284+
* 2. Create a new token with "User" type
285+
* 3. Configure the token prefix and header as needed
286+
* Learn more: https://www.intercom.com/help/en/articles/6615543-setting-up-data-connectors-authentication
287+
*
288+
* This should be called after any user login takes place.
289+
*
290+
* @param authTokens An object containing auth token names and values (e.g., { security_token: "jwt_here" })
291+
* @return {Promise<boolean>} A promise that resolves to true on success.
292+
*/
293+
setAuthTokens(authTokens: { [key: string]: string }): Promise<boolean>;
294+
280295
/**
281296
* [Android Only] Bootstrap event listeners for Android. Call this before setting up your own NativeEventEmitter
282297
*
@@ -332,6 +347,7 @@ const Intercom: IntercomType = {
332347
setLogLevel: (logLevel) => IntercomModule.setLogLevel(logLevel),
333348
setThemeMode: (themeMode) => IntercomModule.setThemeMode(themeMode),
334349
setUserJwt: (jwt) => IntercomModule.setUserJwt(jwt),
350+
setAuthTokens: (authTokens) => IntercomModule.setAuthTokens(authTokens),
335351

336352
bootstrapEventListeners: () => {
337353
if (Platform.OS === 'android' && IntercomEventEmitter?.startEventListener) {

0 commit comments

Comments
 (0)