In the example bellow, is there a way to get the user id (uid) of the user who wrote to 'offers/{offerId}'? I tried to do as described here but it doesn't work in Firestore.
exports.onNewOffer = functions.firestore
.document('offers/{offerId}')
.onCreate(event => {
...
});
As already suggested above, the workaround will be to add a user_id field with the data itself and then read it on the server.
The drawback with this approach will be a security loophole. As we are not verifying the user id on the server, any other user will be able to impersonate other users by sending their id with the data.
For security critical applications, the solution for this will be to use security rules to verify that the correct user_id has been sent with the data
Summary of how I solved this / a workable solution:
On client
Add logged in/current user's uid (e.g. as
creatorId
) to entity they're creating. Access this uid by storing thefirebase.auth().onAuthStateChanged()
User object in your app state.In Firebase Firestore/Database
Add a Security Rule to
create
to validate that the client-suppliedcreatorId
value is the same as the authenticated user's uid; Now you know the client isn't spoofing thecreatorId
and can trust this value elsewhere.e.g.
In Firebase Functions
Add an
onCreate
trigger to your created entity type to use the client-supplied, and now validated,creatorId
to look up the creating user's profile info, and associate/append this info to the new entity doc.This can be accomplished by:
Creating a
users
collection and individualuser
documents when new accounts are created, and populating the newuser
doc with app-useful fields (e.g.displayName
). This is required because the fields exposed by the Firebase Authentication system are insufficient for consumer app uses (e.g.,displayName
andavatarURL
are not exposed) so you can't just rely on looking up the creating user's info that way.e.g. (using ES6)
creatorId
value, and usefuluser
objects, add anonCreate
trigger to your entity type (or all your created entities) to look up the creating user's info and append it to the created object.This clearly leads to a lot of duplicated user info data throughout your system -- and there's a bit of clean up you can do ('creatorId` is duplicated on the created entity in the above implementation) -- but now it's super easy to show who created what throughout your app, and appears to be 'the Firebase way'.
Hope this helps. I've found Firebase to be super amazing in some ways, and make some normally easy things (like this) harder than they 'should' be; on balance though am a major fan.
Until this is added to firestore functions a workaround is to add the
user_id
as a field when creating a document then deleting after. You can then grab it in the function onCreate then after you use it for what you need it for, while still in the function, just delete the field from that document.I was struggling on this for a while and finally contacted the firebase Support:
The
event.auth.uid
is undefined in the event object for firestore database triggers. (It works for the realtime Database Triggers)When I
console.log(event)
I can’t find anyauth
in the output.The official support answer:
I hope this saves someone a few hours.
UPDATE:
The issue has been closed and the feature will never be implemeted:
You can get the current signed-in user tokenId by calling getIdToken() on the User: https://firebase.google.com/docs/reference/functions/functions.auth.UserInfo
This should do the trick: