Skip to content

Commit eb6d87e

Browse files
committed
validate api key
1 parent 773ccd6 commit eb6d87e

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ Sets the user hash necessary for validation when Identity Verification is enable
757757

758758
Initialize the Intercom SDK manually. This is useful when you want to delay initialization until after your app has started, or when using Expo with the `useManualInit` plugin option.
759759

760+
**Important:** This method configures the SDK but does NOT validate your credentials with Intercom's servers. Invalid API keys or App IDs will only be detected when you attempt to use Intercom features (e.g., login, show messenger). The method will return `true` if the SDK is successfully configured, regardless of credential validity.
761+
760762
### Options
761763

762764
| Name | Type | Required | Description |

android/src/newarch/IntercomModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public void onFailure(@NonNull IntercomError intercomError) {
630630
public void initialize(String apiKey, String appId, Promise promise) {
631631
try {
632632
Activity activity = getCurrentActivity();
633-
if (activity != null) {
633+
if (activity != null && activity.getApplication() != null) {
634634
IntercomModule.initialize(activity.getApplication(), apiKey, appId);
635635
promise.resolve(true);
636636
} else {

android/src/oldarch/IntercomModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public void onFailure(@NonNull IntercomError intercomError) {
607607
public void initialize(String apiKey, String appId, Promise promise) {
608608
try {
609609
Activity activity = getCurrentActivity();
610-
if (activity != null) {
610+
if (activity != null && activity.getApplication() != null) {
611611
IntercomModule.initialize(activity.getApplication(), apiKey, appId);
612612
promise.resolve(true);
613613
} else {

examples/expo-example/app/(tabs)/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function App() {
4747
INTERCOM_CONFIG.appId
4848
);
4949
setIsInitialized(true);
50+
setInitError(null);
5051
console.log('Intercom initialized successfully');
5152
}
5253
} catch (error) {

src/index.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,50 @@ export type IntercomType = {
323323
};
324324

325325
const Intercom: IntercomType = {
326-
initialize: (apiKey, appId) => IntercomModule.initialize(apiKey, appId),
326+
initialize: (apiKey, appId) => {
327+
if (!apiKey || typeof apiKey !== 'string') {
328+
return Promise.reject(
329+
new Error('Intercom: apiKey is required and must be a string')
330+
);
331+
}
332+
333+
const isIOS = Platform.OS === 'ios';
334+
const isAndroid = Platform.OS === 'android';
335+
336+
if (isIOS) {
337+
if (!apiKey.startsWith('ios_sdk-')) {
338+
return Promise.reject(
339+
new Error('Intercom: iOS API key must start with "ios_sdk-"')
340+
);
341+
}
342+
if (apiKey.length < 48) {
343+
return Promise.reject(
344+
new Error('Intercom: iOS API key must be at least 48 characters long')
345+
);
346+
}
347+
} else if (isAndroid) {
348+
if (!apiKey.startsWith('android_sdk-')) {
349+
return Promise.reject(
350+
new Error('Intercom: Android API key must start with "android_sdk-"')
351+
);
352+
}
353+
if (apiKey.length < 52) {
354+
return Promise.reject(
355+
new Error(
356+
'Intercom: Android API key must be at least 52 characters long'
357+
)
358+
);
359+
}
360+
}
361+
362+
if (!appId || typeof appId !== 'string' || appId.trim() === '') {
363+
return Promise.reject(
364+
new Error('Intercom: appId is required and must be a non-empty string')
365+
);
366+
}
367+
368+
return IntercomModule.initialize(apiKey, appId);
369+
},
327370
loginUnidentifiedUser: () => IntercomModule.loginUnidentifiedUser(),
328371
loginUserWithUserAttributes: (userAttributes) =>
329372
IntercomModule.loginUserWithUserAttributes(userAttributes),

0 commit comments

Comments
 (0)