Skip to content

Commit a6f9f51

Browse files
authored
Merge pull request #169 from oluademola/tenant-tokenv0.30
update tenant token demo to v0.30
2 parents 875e40c + 855bde5 commit a6f9f51

File tree

15 files changed

+322
-324
lines changed

15 files changed

+322
-324
lines changed

src/tenant-token/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ See our [Documentation](https://docs.meilisearch.com/learn/security/tenant_token
4242

4343
## 🎁 Content of this repository
4444
In this repository, you will find three sub directories:
45-
1. **Frontend** - React App to search,create token and switch between tokens.
45+
1. **Frontend** - React App to search, create token and switch between tokens.
4646
2. **Backend** - Express App to handle Apis.
4747
3. **Seed** - Script to feed intial data in meilisearch.
4848

4949
## 🎬 Getting Started
5050

51-
>Prerequisite: Start Docker and download latest Meilisearch docker image using the command `docker pull getmeili/meilisearch:latest` before going further.
51+
>Prerequisite: Start Docker and download latest Meilisearch docker image using the command `docker pull getmeili/meilisearch:v0.30.4` before going further.
5252
5353
### 1. Seed Data
5454

5555
Navigate to `/seed` and create a **.env** file with content
5656
```
57-
MEILI_API_KEY=<PUT_YOUR_API_KEY_HERE>
57+
MEILI_MASTER_KEY=<PUT_YOUR_MASTER_KEY_HERE>
5858
```
5959
Then run `npm run meilisearch-serve` to start meilisearch on docker.
6060

@@ -68,11 +68,11 @@ npm start
6868

6969
## 2. Backend
7070

71-
Navigate to `/backend` and create a **.env** file with content
71+
Navigate to `/backend` and create a **.env** file with content.
7272

7373
```
7474
MEILI_HOST=http://localhost:7700
75-
MEILI_API_KEY=<PUT_YOUR_API_KEY_HERE>
75+
MEILI_ADMIN_API_KEY=<PUT_YOUR_ADMIN_API_KEY_HERE>
7676
```
7777
Run the following commands:
7878

@@ -81,17 +81,17 @@ npm i
8181
npm start
8282
```
8383

84-
This will start your express server on [http://localhost:5000](http://localhost:5000)
84+
This will start your express server on [http://localhost:5001](http://localhost:5000)
8585

8686
## 3. Frontend
8787

8888
Navigate to `/frontend` and create a **.env** file with content
8989

9090
```
9191
REACT_APP_MEILI_HOST=http://localhost:7700
92-
REACT_APP_MEILI_API_KEY=<PUT_YOUR_API_KEY_HERE>
92+
REACT_APP_MEILI_SEARCH_API_KEY=<PUT_YOUR_SEARCH_API_KEY_HERE>
9393
REACT_APP_MEILI_INDEX=tenant_token
94-
REACT_APP_API_HOST=http://localhost:5000
94+
REACT_APP_API_HOST=http://localhost:5001
9595
```
9696

9797
Run the following commands:

src/tenant-token/backend/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MEILI_HOST=
2-
MEILI_API_KEY=
1+
MEILI_HOST=http://localhost:7700
2+
MEILI_ADMIN_API_KEY=<PUT_YOUR_ADMIN_API_KEY_HERE>

src/tenant-token/backend/index.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const app = express();
66

77
const client = new MeiliSearch({
88
host: process.env.MEILI_HOST || "http://localhost:7700",
9-
apiKey: process.env.MEILI_API_KEY || "masterKey",
9+
apiKey: process.env.MEILI_ADMIN_API_KEY,
1010
});
1111

1212
app.use(cors());
@@ -21,7 +21,8 @@ app.get("/create-tenant-token", async (req, res) => {
2121
/* Replace this comment with the API request */
2222

2323
const { results } = await client.getKeys();
24-
const apiKey = results[0].key;
24+
const apiKey = results.filter((res) => res.name === "Default Search API Key")[0].key;
25+
const apiKeyUid = results.filter((res) => res.name === "Default Search API Key")[0].uid;
2526

2627
const payload = {
2728
tenant_token: {
@@ -33,29 +34,15 @@ app.get("/create-tenant-token", async (req, res) => {
3334
new Date().setFullYear(new Date().getFullYear() + 1)
3435
);
3536

36-
const tenantToken = client.generateTenantToken(payload, {
37+
const tenantToken = client.generateTenantToken(apiKeyUid, payload, {
3738
apiKey,
3839
expiresAt,
3940
});
4041
return res.json({ token: tenantToken });
4142
});
4243

43-
const port = process.env.PORT || 5000;
44+
const port = process.env.PORT || 5001;
4445

4546
app.listen(port, async () => {
46-
console.log("Checking for an API Key");
47-
const { results } = await client.getKeys();
48-
const apiKeyList = results.filter((res) => res.description === "SEARCH");
49-
50-
if (apiKeyList.length === 0) {
51-
await client.createKey({
52-
description: "SEARCH",
53-
actions: ["search"],
54-
indexes: ["tenant_token"],
55-
expiresAt: "2025-01-01T00:00:00Z",
56-
});
57-
console.log("Created API Key");
58-
}
59-
6047
console.log(`Server started at PORT: ${port}`);
6148
});

src/tenant-token/backend/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tenant-token/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"cors": "^2.8.5",
88
"dotenv": "^16.0.0",
99
"express": "^4.17.3",
10-
"meilisearch": "^0.25.0"
10+
"meilisearch": "^0.30.0"
1111
},
1212
"scripts": {
1313
"start": "node index.js",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
REACT_APP_MEILI_HOST=http://localhost:7700
2-
REACT_APP_MEILI_API_KEY=<PUT_YOUR_API_KEY_HERE>
2+
REACT_APP_MEILI_SEARCH_API_KEY=<PUT_YOUR_SEARCH_API_KEY_HERE>
33
REACT_APP_MEILI_INDEX=tenant_token
4-
REACT_APP_API_HOST=http://localhost:5000
4+
REACT_APP_API_HOST=http://localhost:5001

0 commit comments

Comments
 (0)