Hi everyone, I have a system that I have different...
# community-help
m
Hi everyone, I have a system that I have different users and these users have different id companies, when I add a document in typesense it shows it to everyone and that can't happen. How can I filter by user's company ID?
j
You can use scoped api keys to restrict which users have as to specific records: https://typesense.org/docs/0.22.2/api/api-keys.html#generate-scoped-search-key
m
Can I do this on front-end? something like a filter in hits. I think that is the only way that I can get the company ID. I'm using typesense with firebase
j
You would have to generate the scoped api key with an embedded filter on the backend so you don't expose the parent search key. Then you'd use the scoped api key on the FE
m
That's the point, we don't have backend 😆, only front-end + firebase.
j
Firebase would be your backend… you'd create these scoped api keys in your Firebase functions
m
Oh, I see, so the response, do I put the key here? const typesenseInstantsearchAdapter = new
TypesenseInstantSearchAdapter({
server: {
apiKey: // scoped api key
...
j
Correct
m
I see, I think the only problem is get the company ID when the user log in, because firebase cloud functions doesn't have any trigger to show who is the user who is currently logged in.
j
When the user logs in, you could have the same function that handles the login also create a scoped api key for that user and send it to the frontend. Similarly, when a logged in user loads the page, the function that checks if the user is logged in or not, can also create a scoped api key for the user if they are logged in. Or may be there's a separate function that just generates scoped API keys for logged in users. The FE makes calls to this function for logged in users
m
hm, I didn't know that, I'II check out. thanks for the help dude, I really appreciated that
j
Happy to help!
m
When you said the same function that handles login, are you talking about that? https://firebase.google.com/docs/auth/web/password-auth
j
Actually yeah, whatever function that code runs in
m
Hey again, I don't know if I'm doing it right, but I'm doing it
and i get this error
j
That method doesn't work in the browser
It will only run in a nodejs environment
m
Oh, it's because all the solutions you mentioned like the handle login function, or checking if the user is connected, all of them are done by the front end, this is my difficulty at the moment :s
j
Oh I see... Is there no way to check if a user is logged in or not in a Firebase function?
m
No 🙁
j
Looks like there's a workaround: https://stackoverflow.com/a/56023902/123545
Within that function, once you've authenticated the user, you'd generate a scoped api key for that user