In a Firebase web app I am working on I want to get a user ID from a mail address. For that I am trying to write a cloud function. But it is not working or I am not using it properly. Here is the current code (based on some sample I found on the net):
"use strict"; import * as functions from "firebase-functions"; import * as admin from "firebase-admin"; admin.initializeApp(functions.config().firebase); exports.myFunc = functions.https.onRequest(function(req, resp) { const from = req.body.sender; admin.auth().getUserByEmail(from) .then(function(userRecord) { console.log("Successfully fetched user data:", userRecord.toJSON()); }) .catch(function(error) { console.log("Error fetching user data:", error); }); }); I see no issue when running "firebase deploy". Then I try to test the function, various ways as I did with the demo app I wrote following this tutorial.
For example (with both existing and non-existing mail addresses):
https://us-central1-myapp.cloudfunctions.net/myFunc https://us-central1-myapp.cloudfunctions.net/[email protected] But in either cases I get nothing in the web console and this in the browser:
Error: could not handle the request Any suggestion from a more experienced Firebase cloud function user would be welcome.
In fact some tutorial with sample code showing how to use getUserByEmail in a firebase cloud function would be the best.
..... Here is some more information, after further investigations .....
When using this code:
const from = req.body.sender; admin.auth().getUserByEmail(from) I get this error in the logs:
Error fetching user data: FirebaseAuthError: The email address is improperly formatted. When using one of these lines of code:
admin.auth().getUserByEmail("[email protected]") admin.auth().getUserByEmail("[email protected]") I get the expected result in the logs:
This for the first line (with a phony mail):
Error fetching user data: FirebaseAuthError: There is no user record corresponding to the provided identifier. This for the second line (with an actually existing mail):
Successfully fetched user data: { 2022-01-22T07:25:04.946Z ? myFunc: uid: 'yfC123abc....', 2022-01-22T07:25:04.946Z ? myFunc: email: '[email protected]', ..... } What I need is to know the way to correctly have this function accept a parameter (called from or whatever) and also know how to use the function from my original web app.
... Still more code after some more trial and errors ...
Here a new chunk of code showing my current issue:
const from = req.query.from; // The 2 following lines produce the expected result. // That is the mail address passed as a parameter. console.log("from(CL):", from); functions.logger.log("from(FL):", from); admin.auth().getUserByEmail(from) .then(function(userRecord) { I get an error message, when running firebase deploy on the line with getUserByEmail reading:
src/index.ts:37:33 - error TS2345: Argument of type 'string | string[] | ParsedQs | ParsedQs[] | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'. 37 admin.auth().getUserByEmail(from) ~~~~ What is the correct syntax to use for the line handling the getUserByEmail call.
console.log("Error fetching user data:", error);?firebase functions:logto check your logs? Also refer docs for more details.