Skip to content

Commit

Permalink
fix(insights): guard against user token override while auth token is …
Browse files Browse the repository at this point in the history
…set (#1237)
  • Loading branch information
dhayab committed Jan 24, 2024
1 parent 716e392 commit 190e562
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Expand Up @@ -322,6 +322,7 @@ describe('createAlgoliaInsightsPlugin', () => {
),
});

// Setting an authenticated user token should replace the user token
insightsClient('setAuthenticatedUserToken', 'customAuthUserToken');

const playground = createPlayground(createAutocomplete, {
Expand Down Expand Up @@ -356,15 +357,31 @@ describe('createAlgoliaInsightsPlugin', () => {
}),
]);

insightsClient('setAuthenticatedUserToken', undefined);
// Updating a user token should have no effect if there is
// an authenticated user token already set
insightsClient('setUserToken', 'customUserToken2');

userEvent.type(playground.inputElement, 'b');
await runAllMicroTasks();

expect(searchClient.search).toHaveBeenCalledTimes(2);
expect(searchClient.search).toHaveBeenLastCalledWith([
expect.objectContaining({
params: expect.objectContaining({ userToken: 'customUserToken' }),
params: expect.objectContaining({ userToken: 'customAuthUserToken' }),
}),
]);

// Removing the authenticated user token should revert to
// the latest user token set
insightsClient('setAuthenticatedUserToken', undefined);

userEvent.type(playground.inputElement, 'c');
await runAllMicroTasks();

expect(searchClient.search).toHaveBeenCalledTimes(3);
expect(searchClient.search).toHaveBeenLastCalledWith([
expect.objectContaining({
params: expect.objectContaining({ userToken: 'customUserToken2' }),
}),
]);
});
Expand Down
Expand Up @@ -183,6 +183,7 @@ export function createAlgoliaInsightsPlugin(
return {
name: 'aa.algoliaInsightsPlugin',
subscribe({ setContext, onSelect, onActive }) {
let isAuthenticatedToken = false;
function setInsightsContext(userToken?: InsightsEvent['userToken']) {
setContext({
algoliaInsightsPlugin: {
Expand All @@ -204,18 +205,26 @@ export function createAlgoliaInsightsPlugin(
setInsightsContext();

// Handles user token changes
insightsClient('onUserTokenChange', setInsightsContext);
insightsClient('onUserTokenChange', (userToken) => {
if (!isAuthenticatedToken) {
setInsightsContext(userToken);
}
});
insightsClient('getUserToken', null, (_error, userToken) => {
setInsightsContext(userToken);
if (!isAuthenticatedToken) {
setInsightsContext(userToken);
}
});

// Handles authenticated user token changes
insightsClient(
'onAuthenticatedUserTokenChange',
(authenticatedUserToken) => {
if (authenticatedUserToken) {
isAuthenticatedToken = true;
setInsightsContext(authenticatedUserToken);
} else {
isAuthenticatedToken = false;
insightsClient('getUserToken', null, (_error, userToken) =>
setInsightsContext(userToken)
);
Expand All @@ -227,6 +236,7 @@ export function createAlgoliaInsightsPlugin(
null,
(_error, authenticatedUserToken) => {
if (authenticatedUserToken) {
isAuthenticatedToken = true;
setInsightsContext(authenticatedUserToken);
}
}
Expand Down

0 comments on commit 190e562

Please sign in to comment.