Skip to content

Commit

Permalink
added consent support for with-jwt config
Browse files Browse the repository at this point in the history
  • Loading branch information
darshan-iterable committed Oct 15, 2024
1 parent 572c90f commit a606fef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
17 changes: 14 additions & 3 deletions react-example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ const HomeLink = styled(Link)`
)
.then((response: any) => response.data?.token)
};
const { setEmail, setUserID, logout, refreshJwtToken } =
initializeWithConfig(initializeParams);
const {
setEmail,
setUserID,
logout,
refreshJwtToken,
toggleAnonUserTrackingConsent
} = initializeWithConfig(initializeParams);

const handleConsent = (consent?: boolean) =>
toggleAnonUserTrackingConsent(consent);

const container = document.getElementById('root');
const root = createRoot(container);
Expand Down Expand Up @@ -100,7 +108,10 @@ const HomeLink = styled(Link)`
path="/embedded-msgs-impression-tracker"
element={<EmbeddedMsgsImpressionTracker />}
/>
<Route path="/aut-testing" element={<AUTTesting />} />
<Route
path="/aut-testing"
element={<AUTTesting setConsent={handleConsent} />}
/>
</Routes>
</RouteWrapper>
</UserProvider>
Expand Down
27 changes: 27 additions & 0 deletions src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface WithJWT {
setUserID: (userId: string) => Promise<string>;
logout: () => void;
refreshJwtToken: (authTypes: string) => Promise<string>;
toggleAnonUserTrackingConsent: (consent: boolean) => void;
}

export interface WithoutJWT {
Expand Down Expand Up @@ -912,6 +913,32 @@ export function initialize(
console.warn('Could not refresh JWT. Try Refresh the JWT again.');
}
});
},
toggleAnonUserTrackingConsent: (consent: boolean) => {
/* if consent is true, we want to clear anon user data and start tracking from point forward */
if (consent) {
anonUserManager.removeAnonSessionCriteriaData();
localStorage.removeItem(SHARED_PREFS_CRITERIA);

localStorage.setItem(SHARED_PREF_ANON_USAGE_TRACKED, 'true');
enableAnonymousTracking();
} else {
/* if consent is false, we want to stop tracking and clear anon user data */
const anonymousUsageTracked = isAnonymousUsageTracked();
if (anonymousUsageTracked) {
anonUserManager.removeAnonSessionCriteriaData();

localStorage.removeItem(SHARED_PREFS_CRITERIA);
localStorage.removeItem(SHARED_PREF_ANON_USER_ID);
localStorage.removeItem(SHARED_PREF_ANON_USAGE_TRACKED);

typeOfAuth = null;
authIdentifier = null;
/* clear fetched in-app messages */
clearMessages();
}
localStorage.setItem(SHARED_PREF_ANON_USAGE_TRACKED, 'false');
}
}
};
}
Expand Down

0 comments on commit a606fef

Please sign in to comment.