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

Instantsearch and meilisearch tokens #1031

Open
sabatale opened this issue Mar 13, 2023 · 4 comments
Open

Instantsearch and meilisearch tokens #1031

sabatale opened this issue Mar 13, 2023 · 4 comments
Labels
support Question that may be answered by the support team

Comments

@sabatale
Copy link

While running an app with ExpressJS, we started generating tokens for instant-meilisearch from the backend.

However the API key is never recognized as valid. Can you please confirm tokens work the same for instantsearch?

E.g.,

app.get("/mysearch", function (req, res) {
    const client = new MeiliSearch({ host: host, apiKey: key });
    const expiresAt = new Date(new Date().getTime() + (24 * 60 * 60 * 1000)); // Today + 24hrs

    const token = client.generateTenantToken(uuidv4(), {
      apiKey: key,
      expiresAt: expiresAt,
    })

    res.status(200).send({ token: token });
});
  const search = instantsearch({
    indexName: 'indexname',
    searchClient: instantMeiliSearch(
      host,
      token,
      {
        finitePagination: true,
      }
    ),
  })
@bidoubiwa bidoubiwa added the support Question that may be answered by the support team label Mar 14, 2023
@oluademola
Copy link

Hello @sabatale,

Could you please provide the code snippet used in generating your tokens? I see the one above, but it doesn't contain a search rule; the search rule is a required parameter you must pass to generate a valid tenant token.

@sabatale
Copy link
Author

Hey there,

We tried using basic rules:

  /*const searchRules = {
    indexName: {
      filter: '*'
    }
  };*/

  const searchRules = {
    indexName: {
      filter: 'type = 1 AND rating = "Warning"'
    }
  };

  const token = client.generateTenantToken(uuidv4(), searchRules, {
      apiKey: meilikey,
      expiresAt: expiresAt,
    })

Generating an authorization header similar to:
authorization: Bearer [36characs].[143characs].[43characs]

Resulting in:

The provided API key is invalid.

@oluademola
Copy link

@sabatale thank you for the additional information. Sorry if I wasn't clear with my question earlier. To help me identify the source of the problem, I need to see how the token is generated in your codebase and how you search using the token. However, here are a few possibilities you could look into:

  1. The API key used to generate the token must be a valid Meilisearch API key with access to the search action.
  2. The generated token has access to only the index specified in the search rule. For example, if you have specified patient_medical_records as the index name in your search rule, you should remember that the token generated will only be valid for this index.
  3. Verify that you're searching on the correct index as mentioned above in your front end.

Let me know your thoughts. Thanks.

@revskill10
Copy link

How to use with normal meilisearch.js like this ?

React.useEffect(() => {
        setLoading(true)
        searchClient.index(index).getDocument(productId)
        .then(res => {
            setLoading(false)
            setProduct(res)
        })
        .catch(err => {
            setLoading(false)
            setProduct(null)
        })
    }, [index, productId])
    ```
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Question that may be answered by the support team
Projects
None yet
Development

No branches or pull requests

4 participants