Skip to content

Updating Secured API key for react-instantsearch isn't updating the URL for insights (analytics) network call with correct API key #6623

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

Open
1 task done
arindam1997007 opened this issue Apr 8, 2025 · 0 comments · May be fixed by #6631
Labels
triage Issues to be categorized by the team

Comments

@arindam1997007
Copy link

arindam1997007 commented Apr 8, 2025

🐛 Current behavior

Hi team

I am not sure whether this is a bug or not, but I was unable to find the documentation pertaining to react-instantsearch with updating Secured API key.

So, we have implemented SECURED API key to fetch the key with an expiry time from our backend. We are fetching and updating the API key when we get the error : Invalid Application-ID or API key while doing a query to Algolia.

We have insights={true} but we can see that the event analytics is still pointing to the previous incorrect API key. We are getting correct data from the server for Search, but the analytics is getting 401 Unauthorized error.
This is our implementation of updating the key for searchClient

const { addMiddlewares, error } = useInstantSearch();

  useEffect(() => {
    const middleware = ({ instantSearchInstance }) => {
      function handleError(searchError) {

        if (searchError.message === "Invalid Application-ID or API key") {
          CLIENT.setClientApiKey({
            apiKey: newAPIKey,
          });

          instantSearchInstance.refresh();
        }
      }

      return {
        subscribe() {
          instantSearchInstance.addListener("error", handleError);
        },
        unsubscribe() {
          instantSearchInstance.removeListener("error", handleError);
        },
      };
    };

    return addMiddlewares(middleware);
  }, [addMiddlewares]);

Also, one more additional question for this implementation:

Since we get the expiry time in our API key, is there a way we can check the expiry before the ALGOLIA query is called? And then can we set the new API key for making the query?

P.S.: In the Codesandbox, I am not fetching a key from backend, but I am just using an incorrect API key at the start. Then, when I get the invalid API key error, I update the API with the corrrect data.

🔍 Steps to reproduce

  1. Go to https://mvkzz9.csb.app/
  2. Open the network tab
  3. Click on any filter. See the query is coming correct, but the events network call getting 401 error due to the previous incorrect API key

Live reproduction

https://codesandbox.io/p/sandbox/mvkzz9

💭 Expected behavior

Analytic Events at first will be denied due to incorrect API key, but after updating the API key, it should work properly (the query param with the API key should get updated).

Package version

"algoliasearch": "^5.11.0", "react-instantsearch": "7.15.5", "react": "^18.3.1", "react-dom": "^18.3.1",

Operating system

MacOs 15.4

Browser

Microsoft Edge Version 134.0.3124.95 (Official build) (arm64)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@arindam1997007 arindam1997007 added the triage Issues to be categorized by the team label Apr 8, 2025
@aymeric-giraudet aymeric-giraudet linked a pull request May 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Issues to be categorized by the team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant