Skip to content

Commit c6548d2

Browse files
authored
Merge pull request #779 from Iterable/feature/SDK-151-cannot-read-property-authtoken-of-undefined
[SDK-151] cannot-read-property-authtoken-of-undefined
2 parents b935b46 + 4bcbb18 commit c6548d2

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/core/classes/Iterable.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ import { IterableLogger } from './IterableLogger';
2323

2424
const RNEventEmitter = new NativeEventEmitter(RNIterableAPI);
2525

26+
/**
27+
* Checks if the response is an IterableAuthResponse
28+
*/
29+
const isIterableAuthResponse = (
30+
response: IterableAuthResponse | string | undefined | null
31+
): response is IterableAuthResponse => {
32+
if (typeof response === 'string') return false;
33+
if (
34+
response?.authToken ||
35+
response?.successCallback ||
36+
response?.failureCallback
37+
) {
38+
return true;
39+
}
40+
return false;
41+
};
42+
2643
/* eslint-disable tsdoc/syntax */
2744
/**
2845
* The main class for the Iterable React Native SDK.
@@ -952,33 +969,31 @@ export class Iterable {
952969
// Promise result can be either just String OR of type AuthResponse.
953970
// If type AuthReponse, authToken will be parsed looking for `authToken` within promised object. Two additional listeners will be registered for success and failure callbacks sent by native bridge layer.
954971
// Else it will be looked for as a String.
955-
if (typeof promiseResult === typeof new IterableAuthResponse()) {
956-
Iterable.authManager.passAlongAuthToken(
957-
(promiseResult as IterableAuthResponse).authToken
958-
);
972+
if (isIterableAuthResponse(promiseResult)) {
973+
Iterable.authManager.passAlongAuthToken(promiseResult.authToken);
959974

960975
setTimeout(() => {
961976
if (
962977
authResponseCallback === IterableAuthResponseResult.SUCCESS
963978
) {
964-
if ((promiseResult as IterableAuthResponse).successCallback) {
965-
(promiseResult as IterableAuthResponse).successCallback?.();
979+
if (promiseResult.successCallback) {
980+
promiseResult.successCallback?.();
966981
}
967982
} else if (
968983
authResponseCallback === IterableAuthResponseResult.FAILURE
969984
) {
970985
// We are currently only reporting JWT related errors. In
971986
// the future, we should handle other types of errors as well.
972-
if ((promiseResult as IterableAuthResponse).failureCallback) {
973-
(promiseResult as IterableAuthResponse).failureCallback?.();
987+
if (promiseResult.failureCallback) {
988+
promiseResult.failureCallback?.();
974989
}
975990
} else {
976991
IterableLogger?.log('No callback received from native layer');
977992
}
978993
}, 1000);
979994
} else if (typeof promiseResult === 'string') {
980995
//If promise only returns string
981-
Iterable.authManager.passAlongAuthToken(promiseResult as string);
996+
Iterable.authManager.passAlongAuthToken(promiseResult);
982997
} else {
983998
IterableLogger?.log(
984999
'Unexpected promise returned. Auth token expects promise of String or AuthResponse type.'

0 commit comments

Comments
 (0)