Accessing user authentication information within a Cloud Firestore triggered Cloud Function [duplicate] - firebase

This question already has answers here:
Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?
(9 answers)
Does the firebase cloud functions context.auth field work for firestore? [duplicate]
(1 answer)
Closed last month.
I need to get signed in user id when a Cloud Function listening to change in db is triggered
I tried firestore.getAuth() without success.

I need to get signed in user id when a Cloud Function listening to
changes in db is triggered
It is not possible with Cloud Functions for Firestore to access the details of the authenticated user who did the modification that triggered the CF. One workaround is to write the user ID in a field in the Firestore document.
FYI note that, in the other hand, this is possible with the Realtime Database.

Related

Check Logged in User inside Firebase functions (Not inside Flutter or web) [duplicate]

This question already has answers here:
Cloud Functions for Firebase - Get current user id [duplicate]
(1 answer)
How to protect firebase Cloud Function HTTP endpoint to allow only Firebase authenticated users?
(9 answers)
Check if User is Logged in on Firebase Auth from Node.js (Cloud Functions)
(1 answer)
How to check for authenticated users on Google Cloud Function
(1 answer)
Closed 10 months ago.
I would like to know the following
How to get the Logged in User id inside firebase functions
How to check a custom claim inside a firebase function
I know, we can check the auth inside callable function, but how about https functions
A help will be really appreciated
For https functions, you'll have to explicitly pass user's ID Token in your HTTP request (e.g. in Authorization header). Then you can verify that ID token using Firebase Admin SDK which returns a DecodedIdToken object that contains user's custom claims.
Checkout the documentation on how to retrieve user's ID Token and for more information.

Firebase Auth update trigger? [duplicate]

This question already has answers here:
Firebase auth onUpdate cloud function for when a user updates their email
(2 answers)
Firebase Firestore cloud function to watch user email updated
(3 answers)
Closed 1 year ago.
I see that Firebase Auth has documented triggers for user creation and deletion but I don't see a corresponding trigger for updates. For instance, when the display_name for a user is updated is there a way to trigger a function for this event?

Does the firebase cloud functions context.auth field work for firestore? [duplicate]

This question already has answers here:
Firebase Cloud Firestore trigger context.auth is always null [duplicate]
(1 answer)
Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?
(9 answers)
Closed 1 year ago.
Is it possible to get the uid of the user who triggered a cloud function through the context.auth property when using firestore?
exports.updateReport = functions.firestore
.document('groups/{groupId}/reports/{nursingId}')
.onUpdate((change, context) => {
const uid : string | undefined = context.auth?.uid;
})
I found a lot of old questions concerning the same problem with many different responses. (the feature works, the feature will maybe sometime work or that the feature will never work)
In my local setup I context.auth is always undefined so I don't know if I am making a mistake or it doesn't even work at all. And if that is the case, is there any other secure way to get the uid of the user, who triggered the corresponding cloud function?
No, this is not possible with Firestore Cloud Functions: For these Cloud Functions the context parameter does not contain the authentication information for the user that triggered the function.
Note that it is possible with Realtime Database Cloud Functions: they provide authentication information for the user that triggered the function via the context parameter.
One workaround is to have a field in the Firestore doc that contains the user uid.
If you use this workaround, depending on your exact use case (you wrote "is there any other secure way" in your question), you may write a security rule validation that checks that this field does contain the user's uid (e.g. request.resource.data.userId == request.auth.uid).

How to get email of any user from the UID of Firebase in Flutter? [duplicate]

This question already has answers here:
How to get Emails of users via UID in Firebase
(1 answer)
How do I return a list of users if I use the Firebase simple username & password authentication
(7 answers)
Closed 2 years ago.
I need to get the email of any user registered in Firebase Authentication from their UID. Is that possible?
It's not possible using the APIs provided by Firebase Auth web and mobile SDKs (including Flutter). You would have to either:
Store data relevant data in a database and perform the lookup there, or
Implement a backend endpoint that uses the Firebase Admin SDK to perform the lookup

Firebase authentication aggregate data [duplicate]

This question already has an answer here:
Add additional data to user profile via firestore functions onCreate
(1 answer)
Closed 2 years ago.
I have two different applications; let's call them A and B. I use Firestore to store additional user data under a collection called /users.
I have a cloud function that aggregates data whenever a user registers through Google, manual or Facebook to the users collection.
functions.auth.user().onCreate( (user, ctx) => {
//aggregate data in /users
});
The problem is that I want to add another collection, say different-users that are only added when using application B. According to Firebase documentation:
You cannot add other properties to the user object directly; instead,
you can store the additional properties in any other storage services,
like Google Cloud Firestore.
How am I supposed to differentiate these types of users in my cloud function giving the fact that I can't add additional data?
Am I missing something?
The user is created in Firebase Authentication and that is what triggers your Cloud Function. At the point your backend code gets invoked, there is no information anymore about what app called the authentication API.
If you want to store additional information based on where the user was created from, consider calling a Callable Cloud Function directly from within your application code after the creation of the user in Firebase Authentication completes. Alternatively, you could write the info directly to the database from the client, if that works for your use-case.
Also see:
Add additional data to user profile via firestore functions onCreate (which I now see is a duplicate, so I'll close against it in a minute)
Firebase Cloud Function - Create user with extra information

Resources