firebase functions giving unauthorized error

Viewed 39

I am working on react native app which is using firebase and using different firebase services. I am getting [Error: UNAUTHENTICATED] when I call the firebase function from the app. The firebase functions are deployed on firebase. I am trying to make a post in my app that will use the firebase addPost function and post the text data in the firestore DB. This is coming back from the firebase function.

Firestore rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true
    }
  }
}

addPost function

exports.addPost = functions.https.onCall(async (data, context) => {
  console.log(`Adding new post: ${JSON.stringify(data)}`)

  // We set all the db fields for the new post
  const { authorID, postData } = data
  const { postText } = postData
  const author = await fetchUser(authorID)
  console.log("1",author);
  const postID = uuidv4()
  const hashtags = await extractHashtags(postText)
  console.log("1",hashtags);

  const post = {
    id: postID,
    ...postData,
    authorID,
    author: author,
    hashtags,
    createdAt: Math.floor(new Date().getTime() / 1000),
    commentCount: 0,
    reactionsCount: 0,
    reactions: {
      like: [], // the list of userIDs who liked the post
      love: [],
      laugh: [],
      angry: [],
      suprised: [],
      cry: [],
      sad: [],
    },
  }

  // TODO: Compute mentions

  // We insert the new post into canonical collection (source of truth)
  await canonicalPostsRef.doc(postID).set(post, { merge: true })

  // We update all the timelines for the followers / friends of the author (we include the author too, as we want the post to appear in their own timeline)
  // This gets done in trigger.js

  // We update the author user data, with the new post count
  const prevCount = author.postCount || 0
  await updateUser(authorID, { postCount: prevCount + 1 })
})

function called in app

export const addPost = async (postData, author) => { <== data is coming here complete and fine 
  const instance = functions().httpsCallable('addPost')
  try {
    const res = await instance({     <== err after calling
      authorID: author?.id,
      postData: postData,
    })
    console.log("========================="); <== dont run
    console.log(res);
    console.log("=========================");

    return res?.data
  } catch (error) {
    console.log("error===",error) <=== there i get error
    return null
  }
}
0 Answers
Related