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

Getting "The provided API key is invalid." while searching with tenant token #1531

Closed
prateek-ec opened this issue Jul 6, 2023 · 1 comment

Comments

@prateek-ec
Copy link

prateek-ec commented Jul 6, 2023

First of all thank you for the library.

Description
I was trying to implement tenant tokens using meilisearch. I followed the instructions from the document mentioned below:

  1. Generating an API key using the master key
const client = new MeiliSearch({ host: 'http://localhost:7700', apiKey: '4Mkw9S_Yh-X5hTpSS9NLSO2emwKkAa-DZIbpYaV7Hzs' });
client.createKey({
    uid: '5e2442b8-8cb8-4059-904d-dc2d13677c01',
    name: 'Search API Key',
    indexes: ['*'],
    actions: ['search', 'indexes.get'], // I tried with * as well but it didn't work
    expiresAt: null
});
  1. Creating the token and sharing with client
const searchRules = {
    "factors": {
        "filter": "user_name =\"prateek\""
    }
}
const token = client.generateTenantToken('5e2442b8-8cb8-4059-904d-dc2d13677c01', searchRules, {
    apiKey: '5e2442b8-8cb8-4059-904d-dc2d13677c01',
    expiresAt: new Date('2025-12-20'),
});
  1. With the token received, I am trying to fetch the data from an index:
const t = new MeiliSearch({ host: 'http://localhost:7700', apiKey: token })
const response = await t.index(index).search('sample', {
    offset: skip,
    limit: limit,
    filter: ['type = "test"'],
    facets
});

Expected behavior
I should be able to get the data from the index using the token

Current behavior
I am getting the following error during search:
Client side:

MeiliSearchApiError: The provided API key is invalid.
    at /Users/prateek/work/emission_critical/repos/ec-services/node_modules/meilisearch/dist/bundles/meilisearch.cjs.js:186:17
    at step (/Users/prateek/work/emission_critical/repos/ec-services/node_modules/meilisearch/dist/bundles/meilisearch.cjs.js:103:17)
    at Object.next (/Users/prateek/work/emission_critical/repos/ec-services/node_modules/meilisearch/dist/bundles/meilisearch.cjs.js:84:51)
    at fulfilled (/Users/prateek/work/emission_critical/repos/ec-services/node_modules/meilisearch/dist/bundles/meilisearch.cjs.js:74:54)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Meilisearch server side by enabling trace logs:

[2023-07-06T12:09: 18Z DEBUG actix_web:: middleware:: logger] Error in response: ResponseError { code: 403, message: "The provided API key is invalid.", error_code: "invalid_api_key", error_type: "auth", error_link: "https://docs.meilisearch.com/errors#invalid_api_key" }
[2023-07-06T12:09: 18Z INFO  actix_web:: middleware:: logger] :: 1 "POST /indexes/{{index}}/search HTTP/1.1" 403 146 "-" "undici" 0.003958

Screenshots or Logs
None

Environment (please complete the following information):

  • OS: MacOS Ventura Version 13.4.1
  • Meilisearch version: Package version: "1.2.0"
  • meilisearch-js version: 0.33.0
  • Browser: NA
@prateek-ec
Copy link
Author

Found the issue. I was using uid when I was supposed to use key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant