Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(insights): guard against user token override while auth token is set #1237

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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