Scaraux
Scaraux

Reputation: 4172

Firebase on mobile how to authenticate call to cloud function?

I am using Firebase with my React Native app, and I only have anonymous login enabled.

I am trying to call a Cloud Function from the app, but not using the firebase SDK. Instead I want to call it with axios. When I pass the token grabbed from my firebase user as a Bearer token, I get back a 401 Unauthorized.

However, the request goes through if I copy the token I get from the CLI with gcloud auth print-identity-token.

How can I grab/setup a valid token that I could pass to the Cloud Function? Any relevant doc would be appreciated.

Im trying to call the function using a onRequest:

await axios.post(CLOUD_FUNC_URL, payload, {
    headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${TOKEN}`,
  },
});

I am either grabbing the token from gcloud CLI (works) but the one from await auth().currentUser?.getIdToken(); does not.

Upvotes: 1

Views: 227

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

Your function must be private (requires authentication) by default. It works with your token (gcloud auth print-identity-token) because you may be the owner of the project. You can go to Cloud Functions console and add a principal allUsers with Cloud Functions Invoker role so it can be invoked by everyone.

Upvotes: 3

Related Questions