java - 如何使用 Typescript 处理来自云函数 (functions.https.HttpsError) 的客户端 (Android) 错误
问题描述
首先,我使用 Typescript 编写云函数来创建一个具有 Firebase 身份验证的帐户。创建新帐户后,尝试向帐户添加自定义声明并将用户信息添加到 Firebase 实时数据库,这些步骤成功完成。
添加现有帐户时会出现问题。错误打印在函数日志中,但我不能把它扔给 Android
云功能代码:.
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;
}
});
}
解决方案
碰巧您没有使用firebase中链接文档中列出的值作为代码参数您得到Google API的错误代码"failed-auth"
或 "invalid-email"
推荐阅读
- android - 如何在 XML Android Studio 中更改按钮的背景?
- r - 如何在不同的页面上做 ggplot2 facet_wrap?
- reactjs - 我将如何构建一个包含嵌入式 reactjs 前端代码的 spring boot rest api 项目
- reactjs - expo 的 npm 包更新策略
- python - 为什么我收到 NameError: 'Wolf' is not defined
- django - django favicon 图标不显示
- c++ - 从 ESP32 蓝牙库生成的回调访问地图/向量时出现空键/值问题
- microsoft-teams - 从 MS Teams 聊天中调用外部 API
- javascript - 如何根据另一个动态下拉列表和文本框为动态文本框提供默认值?
- ruby - 如何使用 Ruby 初始化二维数组的所有值,有什么方法可以一步完成吗?