title: Cloud functions get current user
body: <p>My problem is the following: I have a system that has some asset information (e.g. a book) and this asset has some characteristics (e.g. title, current owner, color). I want to be able to change current owner by just executing an HTTP request such as presented in the introduction: <a href="https://firebase.google.com/docs/functions/get-started" rel="nofollow noreferrer">https://firebase.google.com/docs/functions/get-started</a></p>
  
  <p>At first it seems simple, however I am unable to get the current user ID or the browser session email, for example. I tried to look for solutions before, using realtime database triggers: <a href="https://firebase.google.com/docs/functions/database-events" rel="nofollow noreferrer">https://firebase.google.com/docs/functions/database-events</a></p>
  
  <p>my current code below:</p>
  
  <pre><code>const functions = require('firebase-functions');
  
  // // Create and Deploy Your First Cloud Functions
  // // https://firebase.google.com/docs/functions/write-firebase-functions
  //
  // exports.helloWorld = functions.https.onRequest((request, response) =&gt; {
  //  response.send("Hello from Firebase!");
  // });
  
  // The Firebase Admin SDK to access the Firebase Realtime Database.
  const admin = require('firebase-admin');
  admin.initializeApp();
  //const user = admin.auth.user();
  
  function dir(object) {
      stuff = [];
      for (s in object) {
          stuff.push(s);
      }
      stuff.sort();
      return stuff;
  }
  
  // Take the text parameter passed to this HTTP endpoint and insert it into the
  // Realtime Database under the path /messages/:pushId/original
  exports.addMessage = functions.https.onRequest(async (req, res) =&gt; {
    // Grab the text parameter.
    const original = req.query.text;
    // Push the new message into the Realtime Database using the Firebase Admin SDK.
    const snapshot = await admin.database().ref('/messages').push({original: original});
    //const snapshot2 = await admin.database().ref('/messages').push({original: email});
    // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
    res.redirect(303, snapshot.ref.toString());
  });
  
  exports.impersonateMakeUpperCase = functions.database.ref('/messages/{pushId}/original')
      .onCreate((snap, context) =&gt; {
        const appOptions = JSON.parse(process.env.FIREBASE_CONFIG);
        appOptions.databaseAuthVariableOverride = context.auth;
        console.log(dir(context))
        const app = admin.initializeApp(appOptions, 'app');
        const uppercase = snap.val().toUpperCase();
        const ref = snap.ref.parent.child('uppercase');
  
        const deleteApp = () =&gt; app.delete().catch(() =&gt; null);
  
        return app.database().ref(ref).set(uppercase).then(res =&gt; {
          // Deleting the app is necessary for preventing concurrency leaks
          return deleteApp().then(() =&gt; res);
        }).catch(err =&gt; {
          return deleteApp().then(() =&gt; Promise.reject(err));
        });
      });
  
  exports.simpleDbFunction = functions.database.ref('/messages/{pushId}/original')
      .onCreate((snap, context) =&gt; {
        if (context.authType === 'ADMIN') {
          // do something
          console.log(snap.val(), 'written by', context);
        } else if (context.authType === 'USER') {
          console.log(snap.val(), 'written by USER', context);
        }
  </code></pre>
  
  <p>Every time I tried to get from context.auth.uid or context.auth.token.email it returns null, it seems that context.auth is null. Maybe there is some step I am skipping.</p>
  
  <p>I just want to get the current user or the browser session user and update the book id i sent in the request for the new owner. I want the interface to be as simple as possible, like scan a bar-code or one-click.</p>