如何使用 flutter 检查电话号码是否已在 firebase 身份验证中注册

Aag*_*hah 4 firebase firebase-authentication flutter

因此,我正在使用 firebase 的电话身份验证的 flutter 应用程序中制作一个简单的注册和登录屏幕。对于注册,我可以注册新用户,因为用户提供了他的电话号码并获得 OTP。但对于登录我想检查输入的号码是否已经注册。如果是这样,他会获得 otp 并登录,如果没有注册,则要求先注册。

Kar*_*ren 6

Firebase 管理 SDK 支持此功能。以下是如何设置 firebase admin(文档)。设置管理员后,您可以使用cloud_functions包从 firebase admin SDK 调用 API,我们将使用的 API 允许我们通过电话号码获取用户(文档)。如果 API 响应是用户记录,我们就知道电话存在。

在此示例中,我使用的是 Node.js。在函数/index.js 中:

exports.checkIfPhoneExists = functions.https.onCall((data, context) => {
   const phone = data.phone
   return admin.auth().getUserByPhoneNumber(phone)
    .then(function(userRecord){
        return true;
    })
    .catch(function(error) {
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

在你的飞镖代码中:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(functionName: 'checkIfPhoneExists');
dynamic resp = await callable.call({'phone': _phone});
if (resp.data) {
    // user exists
}
Run Code Online (Sandbox Code Playgroud)