Learn how to implement secure, multitenant search in your Laravel applications.
meilisearch
driverUser
model that belongs to an Organization
Contact
model that belongs to an Organization
(can only be accessed by users from the same organization)Organization
model that has many User
s and many Contact
sapp/Models/Contact.php
:
app/Models/User.php
:
app/Models/Organization.php
:
User
s can search through data belonging to all Organizations
. To prevent that from happening, you need to generate a tenant token for each organization. You can then use this token to authenticate requests to Meilisearch and ensure that users can only access data from their organization. All User
within the same Organization
will share the same token.
In this guide, you will generate the token when the organization is retrieved from the database. If the organization has no token, you will generate one and store it in the meilisearch_token
attribute.
Update app/Models/Organization.php
:
Organization
model is generating tenant tokens, you need to provide the front-end with these tokens so that it can access Meilisearch securely.
with
method.app/View/Composers/AuthComposer.php
file:
AppServiceProvider
:
meilisearchToken
variable. You use this variable in your front end.
resources/js/vue-app.js
file:
Meilisearch
component you will create next.
The Meilisearch
component is responsible for initializing a Vue Instantsearch client. It uses the @meilisearch/instant-meilisearch
package to create a search client compatible with Instantsearch.
Create it in resources/js/components/Meilisearch.vue
:
Meilisearch
component it in any Blade view by providing it with the tenant token. Don’t forget to add the @vite
directive to include the Vue app in your view.