I use anonymous user in my app and have a function that create user
with only uid
export const createUserObject = functions.auth.user().onCreate((user, context) => {
const userData = {"uid": user.uid}
admin.firestore().collection("users").doc(user.uid).set(userData).then(writeResult => {
console.log('User Created result:', writeResult);
}).catch(err => {
console.log(err);
});
});
Now I want to add another endpoint for user to create note
. I plan to make note it own collection with 2 fields content
and uid
to reference back to user who create it.
Is there a way for Firebase function to retrieve uid
for a triggered user? So I can write something like this, or I have to make user send it along with note?
export const addNote = functions.https.onRequest((request, response) => {
if(request.method !== "POST"){
response.sendStatus(404)
return;
}
const content = request.body.content
const data = {
content: content,
uid: HOW_CAN_I_GET_CURRENT_USER
};
let db = admin.firestore()
return db.collection("notes").add({content: content}).then((ref) => {
response.sendStatus(201)
})
});