Skip to content

Commit 16c2c9a

Browse files
committed
fix: improve JWT error logging and alert messaging for auth failures
1 parent b22b9ea commit 16c2c9a

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

example/src/hooks/useIterableApp.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
IterableInAppShowResponse,
1616
IterableLogLevel,
1717
IterableRetryBackoff,
18+
IterableAuthFailureReason,
1819
} from '@iterable/react-native-sdk';
1920

2021
import { Route } from '../constants/routes';
@@ -134,9 +135,15 @@ export const IterableAppProvider: FunctionComponent<
134135
};
135136

136137
config.onJWTError = (authFailure) => {
137-
console.error('Error fetching JWT:', authFailure);
138+
console.log('onJWTError', authFailure);
139+
140+
const failureReason =
141+
typeof authFailure.failureReason === 'string'
142+
? authFailure.failureReason
143+
: IterableAuthFailureReason[authFailure.failureReason];
144+
138145
Alert.alert(
139-
`Error fetching JWT: ${authFailure.failureReason}`,
146+
`Error fetching JWT: ${failureReason}`,
140147
`Token: ${authFailure.failedAuthToken}`
141148
);
142149
};
@@ -167,20 +174,20 @@ export const IterableAppProvider: FunctionComponent<
167174
config.inAppHandler = () => IterableInAppShowResponse.show;
168175

169176
// NOTE: Uncomment to test authHandler failure
170-
config.authHandler = () => {
171-
console.log(`authHandler`);
172-
173-
return Promise.resolve({
174-
authToken: 'SomethingNotValid',
175-
successCallback: () => {
176-
console.log(`authHandler > success`);
177-
},
178-
// This is not firing
179-
failureCallback: () => {
180-
console.log(`authHandler > failure`);
181-
},
182-
});
183-
};
177+
// config.authHandler = () => {
178+
// console.log(`authHandler`);
179+
180+
// return Promise.resolve({
181+
// authToken: 'SomethingNotValid',
182+
// successCallback: () => {
183+
// console.log(`authHandler > success`);
184+
// },
185+
// // This is not firing
186+
// failureCallback: () => {
187+
// console.log(`authHandler > failure`);
188+
// },
189+
// });
190+
// };
184191

185192
setItblConfig(config);
186193

src/core/enums/IterableAuthFailureReason.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@ export enum IterableAuthFailureReason {
88
* An auth token's expiration must be less than one year from its issued-at
99
* time.
1010
*/
11-
AUTH_TOKEN_EXPIRATION_INVALID = 'AUTH_TOKEN_EXPIRATION_INVALID',
11+
AUTH_TOKEN_EXPIRATION_INVALID,
1212
/** The token has expired. */
13-
AUTH_TOKEN_EXPIRED = 'AUTH_TOKEN_EXPIRED',
13+
AUTH_TOKEN_EXPIRED,
1414
/** Token has an invalid format (failed a regular expression check). */
15-
AUTH_TOKEN_FORMAT_INVALID = 'AUTH_TOKEN_FORMAT_INVALID',
15+
AUTH_TOKEN_FORMAT_INVALID,
1616
/** `onAuthTokenRequested` threw an exception. */
17-
AUTH_TOKEN_GENERATION_ERROR = 'AUTH_TOKEN_GENERATION_ERROR',
17+
AUTH_TOKEN_GENERATION_ERROR,
1818
/** Any other error not captured by another constant. */
19-
AUTH_TOKEN_GENERIC_ERROR = 'AUTH_TOKEN_GENERIC_ERROR',
19+
AUTH_TOKEN_GENERIC_ERROR,
2020
/** Iterable has invalidated this token and it cannot be used. */
21-
AUTH_TOKEN_INVALIDATED = 'AUTH_TOKEN_INVALIDATED',
21+
AUTH_TOKEN_INVALIDATED,
2222
/** The request to Iterable's API did not include a JWT authorization header. */
23-
AUTH_TOKEN_MISSING = 'AUTH_TOKEN_MISSING',
23+
AUTH_TOKEN_MISSING,
2424
/** `onAuthTokenRequested` returned a null JWT token. */
25-
AUTH_TOKEN_NULL = 'AUTH_TOKEN_NULL',
25+
AUTH_TOKEN_NULL,
2626
/**
2727
* Iterable could not decode the token's payload (`iat`, `exp`, `email`,
2828
* or `userId`).
2929
*/
30-
AUTH_TOKEN_PAYLOAD_INVALID = 'AUTH_TOKEN_PAYLOAD_INVALID',
30+
AUTH_TOKEN_PAYLOAD_INVALID,
3131
/** Iterable could not validate the token's authenticity. */
32-
AUTH_TOKEN_SIGNATURE_INVALID = 'AUTH_TOKEN_SIGNATURE_INVALID',
32+
AUTH_TOKEN_SIGNATURE_INVALID,
3333
/**
3434
* The token doesn't include an `email` or a `userId`. Or, one of these
3535
* values is included, but it references a user that isn't in the Iterable
3636
* project.
3737
*/
38-
AUTH_TOKEN_USER_KEY_INVALID = 'AUTH_TOKEN_USER_KEY_INVALID',
38+
AUTH_TOKEN_USER_KEY_INVALID,
3939
}

0 commit comments

Comments
 (0)