How to generate an API key for each user to restrict search (Firebase extension)

Hi! I am using the Firebase extension and it works fine. But I need to restrict the search results so that each user only has access to their data from the index. I understand that the right way to go is generating an API key on the fly for each new user. I have tried many ways based on Firebase and Algolia docs but my cloud functions do not deploy correctly. I get a general Firebase error that deployment failed.

Here is my index.js file in Firebase cloud functions:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();

// For the default version
const algoliasearch = require('algoliasearch');


const db = admin.firestore();


const client = algoliasearch('xxxxxxxxx', 'xxxxxxxxxxxxxxx');
const index = client.initIndex('users');





   exports.generateAlgoliaAPIKey = functions.firestore
    .document('users/{user}')
    .onWrite((change, context) => {

      const userid = 'xxxxxxxxxxxx';

      const publicKey = client.generateSecuredApiKey(
        'xxxxxxxxxxxxxxxxx', // A search key that you keep private
        {
          filters: `userid:${userid}`
        }
      );

      console.log("this is the new key: " + publicKey)


    });

At first glance that code seems okay, would you be able to PM me a private Gist or sandbox with your code in its entirety? The issue may possibly be with some of the other code within the function.

I know when I set up the functions project locally, I opted to use ESLint to help me recognize issues ahead of time. It may be able to help you locate your issue here as well, so it may be worth a shot.

Thank you! I can share a secret Gist with the code. I don’t see any option to PM you here though? How can I do that?

I sent you a PM, it should appear in your notifications, or you can follow this link.