首页 > 解决方案 > 如何使用 Typescript 处理来自云函数 (functions.https.HttpsError) 的客户端 (Android) 错误

问题描述

首先,我使用 Typescript 编写云函数来创建一个具有 Firebase 身份验证的帐户。创建新帐户后,尝试向帐户添加自定义声明并将用户信息添加到 Firebase 实时数据库,这些步骤成功完成。

添加现有帐户时会出现问题。错误打印在函数日志中,但我不能把它扔给 Android

我的 Firebase 云功能日志

云功能代码:.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { User } from './model/User';

admin.initializeApp();

const db = admin.database();

exports.createSellerAccount = functions.https.onCall((data, context) => {
  const userEmail = data.email;
  const userPassword = data.password;
  const user: User = new User();
  const newUserData = JSON.parse(data.newUser);

  user.setFirstName(newUserData.firstName);
  user.setLastName(newUserData.lastName);
  user.setMobileNumber(newUserData.mobileNumber);

  admin.auth().createUser({
    email: userEmail,
    password: userPassword
  }).then(function (userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    const additionalClaims = {
      sellerAccount: true
    };

    admin.auth().setCustomUserClaims(userRecord.uid, additionalClaims)
      .then(function (customToken) {
        // Send token back to client
        console.log("Successfully token created new user:", userRecord.uid);
      })
      .catch((error) => {
        console.log("Error creating custom token:", error);
      });

    db.ref("Users/" + userRecord.uid).set(user)
      .then(() => {
        console.log("seller info inserted successfully");
      })
      .catch(function (error) {
        console.log("Error while inserting seller info:", error);
      });

  }).catch(function(error) {
    // console.log("Error creating new user:", error);
    throw new functions.https.HttpsError('already-exists',error);
  });

})

安卓代码:。

private void createAccount() {
    ekhtarSeller.showProgressDialog(this);
    newUser.setFirstName(tietFirstName.getText().toString().trim());
    newUser.setLastName(tietLastName.getText().toString().trim());
    newUser.setMobileNumber(tietMobileNumber.getText().toString().trim());

    HashMap<String, Object> data = new HashMap<String, Object>();
    data.put("email", tietEmail.getText().toString().trim());
    data.put("password", tietPassword.getText().toString().trim());
    data.put("newUser", new Gson().toJson(newUser));

    mFunctions
            .getHttpsCallable("createSellerAccount")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    ekhtarSeller.getProgressDialog().cancel();
                    String result = (String) task.getResult().getData();
                    Toast.makeText(CreateAccountActivity.this, result, Toast.LENGTH_SHORT).show();
                    return result;
                }
            });
}

标签: javaandroidtypescriptfirebasegoogle-cloud-functions

解决方案


碰巧您没有使用firebase中链接文档中列出的值作为代码参数您得到Google API的错误代码"failed-auth""invalid-email"

请访问 functions.https.HttpsError


推荐阅读