Home > OS >  Firestore: create hidden data when creating new document
Firestore: create hidden data when creating new document

Time:05-06

I'm adding a new document every time a user is logged in with Google authentication and that user does not exist in firestore.

What I want is to add a 'USER' role when creating this new USER. I was expecting to do this outside the react application so it can not be hacked into creating different types of users by calling the Firestore api.

At first I thought of a function but now they are only allowed by having a paid plan.

Thanks in advance for the help.

CodePudding user response:

If you want to implement a role-based access control to your Firestore database, the recommended approach is to use Custom Claims. This indeed requires using the Admin SDK via Cloud Functions or a server you own.

Activating Cloud Functions indeed requires entering the details of a credit card but there is a free tier which allows up to 2M invocations/month for free. So, unless your app is very popular, you'll be only billed for each container required to deploy a function but this is a negligible cost of few tenths of $.


If you don't have any credit card, there is another solution: using some Firestore documents to declare the users roles, as explained here in the doc.

CodePudding user response:

I wanted to add this as a comment but I guess you could make use of the firestore rules to make sure that the role value sent by the client is always set to user and not anything else or you could make sure that this USER role added it a boolean value and in the rules make sure he can only edit his own document this way he won't be able to change his role even if he set it to false it won't give him a different role

  • Related