Skip to content

Commit e65d6af

Browse files
feat: Implement retry token verification feature (#23)
1 parent b3d2814 commit e65d6af

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/components/sirenProvider.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212

1313
import type { SirenProviderConfigProps } from '../types';
1414
import { isNonEmptyArray, logger } from '../utils/commonUtils';
15-
import { events, eventTypes } from '../utils/constants';
15+
import { events, eventTypes, TOKEN_VERIFICATION_FAILED } from '../utils/constants';
1616

1717
type SirenContextProp = {
1818
siren: Siren | null;
@@ -66,6 +66,8 @@ export const useSirenContext = (): SirenContextProp => useContext(SirenContext);
6666
* @param {React.ReactNode} props.children - Child components that will have access to the Siren context.
6767
*/
6868
const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
69+
let retryCount = 0;
70+
6971
const [siren, setSiren] = useState<Siren | null>(null);
7072

7173
useEffect(() => {
@@ -121,11 +123,19 @@ const SirenProvider: React.FC<SirenProvider> = ({ config, children }) => {
121123
return {
122124
token: config.userToken,
123125
recipientId: config.recipientId,
124-
onError: (error: SirenErrorType): void => logger.info(`Error : ${JSON.stringify(error)}`),
126+
onError: retryVerification,
125127
actionCallbacks: actionCallbacks
126128
};
127129
};
128130

131+
const retryVerification = (error: SirenErrorType) => {
132+
if (error.Code === TOKEN_VERIFICATION_FAILED && retryCount < 3)
133+
setTimeout(() => {
134+
initialize();
135+
retryCount++;
136+
}, 5000);
137+
};
138+
129139
// Function to initialize the Siren SDK and fetch notifications
130140
const initialize = (): void => {
131141
const dataParams: InitConfigType = getDataParams();

src/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const ERROR_DESCRIPTION = 'Could not load the notifications. Please refre
7474
export const DEFAULT_WINDOW_TITLE = 'Notifications';
7575
export const RETRY_BUTTON_LABEL = 'Retry';
7676
export const CLEAR_ALL_LABEL = 'Clear All';
77+
export const TOKEN_VERIFICATION_FAILED = 'TOKEN_VERIFICATION_FAILED';
7778

7879
export const errorMap = {
7980
SIREN_OBJECT_NOT_FOUND: {

0 commit comments

Comments
 (0)