36

Spent a while searching out a solution to what seems a simple problem but... How can I get the current users details inside a cloud function (when not using an functions.auth trigger)? I'm sure there must be a simple solution to this. (I hope)

Thanks for the replies... Just done that but how would I get the /user/uid from the Firebase DB within a storage triggered event? e.g.

exports.addUploadInfoToDatabase = functions.storage.object().onChange(event => {
  console.log("firebase storage has been changed");
  // need to find the uid in here somehow
  admin.database().ref(`/user/uid`).push({testKey:"testData"});
})

Applogies if I've missed the point in your replies.

Thanks...

nick clarke
  • 369
  • 1
  • 3
  • 4
  • Hi, welcome to SO! Please show what you have already tried. Your question, as it currently is will probably not receive a good answer, and may be closed. Please [take the tour](https://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask) and [how to create a minimal, verifiable example](https://stackoverflow.com/help/mcve) for better results using this site. – vatavale Aug 10 '17 at 12:56
  • 1
    It's not clear how the answer on the question flagged as the duplicate addresses the OP's question. – Robin Mackenzie Dec 10 '17 at 22:55
  • 1
    For those of you actually looking for an answer to this wrongfully flagged question, see my answer here: https://stackoverflow.com/a/56761633/6276471 – Terren Jun 25 '19 at 20:35

1 Answers1

39

First you can get the current signed-in user tokenId by calling getIdToken() on the User: https://firebase.google.com/docs/reference/js/firebase.User#getIdToken

firebase.auth().currentUser.getIdToken(true)
.then(function (token) {
    // You got the user token
})
.catch(function (err) {
    console.error(err);
});

Then send it to your cloud function.

Then in your cloud function you can use: https://firebase.google.com/docs/auth/admin/verify-id-tokens

Example:

admin.auth().verifyIdToken(idToken)
  .then(function(decodedToken) {
    var uid = decodedToken.uid;
    // ...
  }).catch(function(error) {
    // Handle error
  });

The documentation for decodedToken is here: https://firebase.google.com/docs/reference/admin/node/admin.auth.DecodedIdToken#uid

DecodedIdToken contains the user uid, then you can get the user by calling this: https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth#getUser

Which returns a UserRecord https://firebase.google.com/docs/reference/admin/node/admin.auth.UserRecord

Kim
  • 1,158
  • 8
  • 18
  • 2
    Thanks for the reply... "First you can send the current signed-in user tokenId from the client to your Cloud Function".... Do you mean pass a value to the cloud function? I can't find any documentation to achieve that... – nick clarke Aug 10 '17 at 14:17
  • Added Javascript example to get the user token in client. – Kim Aug 10 '17 at 14:27
  • 2
    How do you access `firebase.auth().currentUser`? I got `firebase is not defined`. If you would already have a `currentUser` why not directly get the `.uid? – Francescu Dec 08 '17 at 10:30
  • 1
    @Francescu `firebase.auth().currentUser` is done on the client side (browser), when initializing your app you do: `` this script automatically creates the `window.firebase` object that you can call as `firebase` from anywhere. – AIon Jan 06 '18 at 16:59
  • 37
    I don't see how the proposed answer solves the problem. The poster is responding to a Firebase Event triggered by Google Cloud Storage, so there is no "client" where they can acquire the id token and pass it to the function. I am trying to solve a similar problem but with an event triggered by Firestore. I need access to the user that performed the Firestore op that caused the event to be triggered. – Max Ross - Google Jan 27 '18 at 05:13
  • 1
    @MaxRoss-Google Indeed. What I do is make the `uid` part of the bucket path, and on the trigger side just parse the path from the event data to get the `uid` again. – opyate Jun 07 '18 at 10:21
  • 10
    Actually it is quite simple to do. Get the `uid` of the user that caused the change: `functions.database.ref('...').onWrite((change, context) => { const uid = context.auth.uid; });` Then get the user using `admin.auth().getUser(uid)`. – Randy Sugianto 'Yuku' Nov 04 '18 at 08:30
  • @RandySugianto'Yuku' can you please explain that idea a little better? – Joao Alves Marrucho Dec 06 '18 at 18:40
  • 2
    const uid = context.auth.uid; – Ayyappa Jun 02 '20 at 05:43