meilisearch / meilisearch-js

JavaScript client for the Meilisearch API

Home Page:https://www.meilisearch.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

prateek-ec opened this issue · comments

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

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