Skip to content

Commit 65cb551

Browse files
committed
feat: add authentication manager to Iterable class
1 parent f1d10cb commit 65cb551

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

src/core/classes/Iterable.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { IterableCommerceItem } from './IterableCommerceItem';
2323
import { IterableConfig } from './IterableConfig';
2424
import { IterableLogger } from './IterableLogger';
2525
import type { IterableAuthFailure } from '../types/IterableAuthFailure';
26+
import { IterableAuthManager } from './IterableAuthManager';
2627

2728
const RNEventEmitter = new NativeEventEmitter(RNIterableAPI);
2829

@@ -89,6 +90,19 @@ export class Iterable {
8990

9091
private static _inAppManager: IterableInAppManager | undefined;
9192

93+
/**
94+
* Authentication manager for the current user.
95+
*
96+
* This property provides access to authentication functionality including
97+
* pausing the authentication retry mechanism.
98+
*
99+
* @example
100+
* ```typescript
101+
* Iterable.authManager.pauseAuthRetries(true);
102+
* ```
103+
*/
104+
static authManager: IterableAuthManager = new IterableAuthManager();
105+
92106
/**
93107
* Initializes the Iterable React Native SDK in your app's Javascript or Typescript code.
94108
*
@@ -1029,7 +1043,7 @@ export class Iterable {
10291043
// 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.
10301044
// Else it will be looked for as a String.
10311045
if (typeof promiseResult === typeof new IterableAuthResponse()) {
1032-
RNIterableAPI.passAlongAuthToken(
1046+
Iterable.authManager.passAlongAuthToken(
10331047
(promiseResult as IterableAuthResponse).authToken
10341048
);
10351049

@@ -1056,9 +1070,9 @@ export class Iterable {
10561070
}, 1000);
10571071
// Use unref() to prevent the timeout from keeping the process alive
10581072
timeoutId.unref();
1059-
} else if (typeof promiseResult === typeof '') {
1073+
} else if (typeof promiseResult === 'string') {
10601074
//If promise only returns string
1061-
RNIterableAPI.passAlongAuthToken(promiseResult as string);
1075+
Iterable.authManager.passAlongAuthToken(promiseResult as string);
10621076
} else {
10631077
Iterable?.logger?.log(
10641078
'Unexpected promise returned. Auth token expects promise of String or AuthResponse type.'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import RNIterableAPI from '../../api';
2+
import { IterableAuthResponse } from './IterableAuthResponse';
3+
4+
/**
5+
* Manages the authentication for the Iterable SDK.
6+
*
7+
* @example
8+
* ```typescript
9+
* const config = new IterableConfig();
10+
* const logger = new IterableLogger(config);
11+
* const authManager = new IterableAuthManager(logger);
12+
* ```
13+
*/
14+
export class IterableAuthManager {
15+
/**
16+
* Pause the authentication retry mechanism.
17+
*
18+
* @param pauseRetry - Whether to pause the authentication retry mechanism
19+
*
20+
* @example
21+
* ```typescript
22+
* const authManager = new IterableAuthManager();
23+
* authManager.pauseAuthRetries(true);
24+
* ```
25+
*/
26+
pauseAuthRetries(pauseRetry: boolean) {
27+
return RNIterableAPI.pauseAuthRetries(pauseRetry);
28+
}
29+
30+
/**
31+
* Pass along an auth token to the SDK.
32+
*
33+
* @param authToken - The auth token to pass along
34+
*/
35+
passAlongAuthToken(
36+
authToken: string | null | undefined
37+
): Promise<IterableAuthResponse | string | undefined> {
38+
return RNIterableAPI.passAlongAuthToken(authToken);
39+
}
40+
}

0 commit comments

Comments
 (0)