Hardik3296
Hardik3296

Reputation: 456

Connecting to cloud function running on emulator from android emulator in react native

I am building an application and using firebase for the backend. I used react-native-firebase to use firebase in my application. I have developed a login cloud function

exports.login = functions.https.onCall((data, context) => {
   console.log("data", data);
   console.log("context", context);
   return "Login successful";
});

On running npm run serve in the functions folder I am getting these configurations in my console. Console output after running npm run serve inside functions folder for firebase

Also I have added the following code to connect to the cloud function from my android emulator running the application.

import functions from "@react-native-firebase/functions"
const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");
    const handleLogin = () => {
        // console.log(email, password);
        functions()
            .useFunctionsEmulator("URL")
            .httpsCallable("login")({ email, password })
            .then((response) => {
                alert(response);
            });
    }

for URL I have tried "http://localhost:5001/" as it is the port on which the functions emulator is listening Port on which cloud functions is listening. But on running the application I am getting this error Error on clicking login button in app console. I tried searching for the error but nothing relevant comes up. Any help will be appreciated.

These are my cloud functions that I have defined

exports.helloWorld = functions.https.onRequest((request, response) => {
   functions.logger.info("Hello logs!", { structuredData: true });
   response.send("Hello from Firebase!");
});

exports.signup = functions.https.onRequest((request, response) => {
   console.log("request", request);
   response.send("SignUp successfull");
});

exports.login = functions.https.onCall((data, context) => {
   console.log("data", data);
   console.log("context", context);
   return "SignIn successfull";
});

Upvotes: 3

Views: 951

Answers (1)

Hardik3296
Hardik3296

Reputation: 456

I was able to work it out finally

const handleLogin = async () => {
        functions().useFunctionsEmulator("http://localhost:5001")
        const response = await functions().httpsCallable("login")({ email, password });
        console.log("response", response);
    }

This is all the code required to successfully call cloud function locally inside an emulator from your running android emulator.

Upvotes: 4

Related Questions