Implementing recaptch using MongoDB - Appservice - function and React

I am trying to implement reCAPTCHA using React and MongoDB App Service. First, I would like to know if there is any possible way to access a function in an App Service without logging in. Alternatively, I have used an API key to log in and then access that function. After that, I log in again using the correct email and password for the account. I am not sure if this is the correct way of handling it.

This is my code :

const handleSubmit = async (event: React.FormEvent) => {

const data = new FormData(event.currentTarget);
const username = await data.get("username");
const password = await data.get("password");

const captchaValue = recaptcha?.current?.getValue();

if (!captchaValue) {
  alert("Please verify the reCAPTCHA!");
} else {
  alert("Form submission successful!");

try {
  const credentials = Realm.Credentials.apiKey(
  const thisuser = await realmApp.logIn(credentials);
  const result = await thisuser?.functions.reCaptcha_Check({
    token: captchaValue,

  await login({ username, password }).catch(() =>
    notify("Invalid email or password")

} catch (error) {
  console.error("Error executing command:", error);