user60786
user60786

Reputation: 397

Getting user info from request to Cloud Function in Firebase

I'd like to be able to get user information for a Cloud Function that gets called in Firebase. I have something like this, which is being called after the user signs into the app:

exports.myFunction = functions.https.onRequest((req, res) => { ... }

And I need to get the uid of the signed-in user that made the request. Any advice or comments please?

Upvotes: 14

Views: 5606

Answers (2)

Mariana Reis Silveira
Mariana Reis Silveira

Reputation: 311

To get the UID, you'll first need to pass the authToken on the request body.

// GET AUTH TOKEN ON THE CLIENT SIDE     
const authToken = await auth.currentUser.getIdToken()

Then, on the cloud function, you verify the token, which will return the UID.

// GET ID OF AUTHENTICATED USER
const { uid } = await admin.auth().verifyIdToken(authToken)

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Information about the user that makes the request is not automatically passed along with HTTPS functions calls.

You can either pass the ID token along yourself and decode/verify it in your function, or you can use a callable HTTPS function, which passes the user information along automatically.

Upvotes: 17

Related Questions