1

I am trying to access the email and uid of current signed in user but this error is shown

const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
const firestore= admin.firestore();
    exports.StripeSource =functions.firestore.document('cards/{data}/tokens/{tokenid}').onCreate(async (tokenSnap,context) => {
      var user = firebase.auth().currentUser;
    var name, email, photoUrl, uid, emailVerified;
      email = user.email;
      uid = user.uid; 
    console.log('User Email is :', email);

      });

Error in fire base log

ReferenceError: firebase is not defined
    at exports.StripeSource.functions.firestore.document.onCreate (/srv/index.js:8:14)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

but changing firebase.auth().currentUser; to admin.auth().currentUser; it works perfectly but recieved error

TypeError: Cannot read property 'email' of undefined
    at exports.StripeSource.functions.firestore.document.onCreate (/srv/index.js:8:16)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

1 Answer 1

3

Change this:

  var user = firebase.auth().currentUser;

into this:

  var user = admin.auth().currentUser;

Check here for more information:

https://firebase.google.com/docs/reference/admin/node/admin.auth

Sign up to request clarification or add additional context in comments.

7 Comments

super awesome sir but I have another issue I just update the question please check it
check here stackoverflow.com/questions/50235846/… you cannot get the current user in cloud functions, also check this link for the methods firebase.google.com/docs/reference/admin/node/admin.auth
Thanks peter. is there any way to access document name set according to user name? I can access a static document name in node js but if I set document name which is different for each user(may be according to name or email address) is there anyway access variable document name in nodejs?
you mean document id in firestore?
Please ask another question as this is not related to this question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.