4

Background

I have some Endpoint APIs on App Engine. I am able to authenticate the Firebase User in my Endpoint API when making a call from my client. (I append the Bearer token to HTTP request header when making the call).

I want to create a Cloud Function that is triggered on User Creation in Firebase (referred this video tutorial). From this Cloud Function, I want to call an Endpoint API using HTTP Request. For the HTTP API call to work, I need to send a Firebase Token in the request header.

Question

How do I get current user's ID Token inside Cloud Function?

A related post here recommends that I should call the API from my Ionic/Angularjs client. However, that will be a roundtrip back to the client and then to Endpoints. I am trying to avoid that.

I found that in /node_modules/firebase-admin/lib/firebase-app.js, there is a call to "FirebaseAppInternals.prototype.getToken". But I am not sure how to call it from my Cloud Function.

Sorry if its a noob question. I am new to Javascript and NodeJs. Any pointers will be of great help!

Also, I am not using Custom Tokens.

Thanks!

sree
  • 43
  • 4
  • currentUserUid = event.auth.variable ? event.auth.variable.uid : null; – mohamad rabee Aug 04 '17 at 12:55
  • Thanks Mohamad! What should I replace "variable" with? Also, won't this give me just the UID? I am trying to get the Auth Token String. I believe the UID is encoded inside the Token itself, but it is not the entire authentication token. – sree Aug 04 '17 at 13:16
  • see https://stackoverflow.com/questions/42750060/getting-the-user-id-from-a-database-trigger-in-cloud-functions-for-firebase – mohamad rabee Aug 04 '17 at 13:21
  • I did a console.log on event and got this: `{ data: { displayName: 'sree', email: 'email@gmail.com', emailVerified: true, metadata: { createdAt: 2017-08-04, lastSignedInAt: 2017-08-04 }, photoURL: 'https://someurl', providerData: [ [Object] ], uid: 'pnbSFi5i2ufzHKJHLhsjgibberish' }, eventId: 'xxxx-xxxxx', eventType: 'providers/firebase.auth/eventTypes/user.create', notSupported: {}, resource: 'projects/myprojectid', timestamp: '2017-08-04', params: {} }` Seems like onCreate event does not have event.auth property. – sree Aug 04 '17 at 13:47
  • `console.log(event.auth)` returns `undefined`. So does `console.log(event.providerData)` Also, I am unable to comment on the other thread because I dont have enough reputation! :( – sree Aug 04 '17 at 13:47
  • Looks like this is not possible. https://stackoverflow.com/questions/44148170/how-to-obtain-access-token-in-cloud-functions-for-firebase-when-user-signs-up?rq=1 – sree Aug 04 '17 at 13:56
  • I am not I understand this clearly. If you want to get the Firebase ID token of the signed in user, you can call on the client after the user is signed in or created `currentUser.getIdToken()`. This will resolve with the ID token which you can then send to your cloud functions HTTP endpoint. – bojeil Aug 05 '17 at 21:40

0 Answers0