Blasco
Blasco

Reputation: 1745

Write in firestore from a firebase callable function as if it were the user provided by the auth context

In firebase callable functions we get a handy context parameter which includes the auth data, including the uid. I have my firestore rules properly defined, which work based on the logged in user. Is there a way to use this context to write from the server as if it were the user, so the rules apply?

A simple example:

export const firestoreCreateDocumentOnCall = functions
  .region(...regions).https.onCall(async (data, context) => {
    try {
      const path = data.path as string;
      const docData = data.data as any;
      // ! This writes as admin, I would like as if it were the user provided by context !
      await admin.firestore().doc(path).set(docData);
      return;
    } catch(error) {
      console.error(`Error: ${error}`);
      return { error };
    }
  });

Upvotes: 1

Views: 326

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599706

Accessing Firestore from within Cloud Functions happens through the Admin SDK, which runs with administrative privileges. There is no way access Firestore as an alternative user through the Admin SDK.

The only alternative I can think of is accessing Firestore through its REST API, but I've never tried this and it's definitely less convenient than using an SDK.

Also see:

Upvotes: 1

Related Questions