如何通过电话号码查询Firebase身份验证?

时间:2020-04-22 14:47:23

标签: firebase firebase-authentication

在我们的React 16.13.0应用程序中,我们正在使用Firebase。我们将用户链接到这样的电话号码

return firebase
  .auth()
  .currentUser.linkWithPhoneNumber(phoneNumber, recaptchaVerfier)
  .then(function (confirmationResult: any) {
    var code = window.prompt("Provide your SMS code");
    recaptchaVerfier.clear();
    return confirmationResult.confirm(code).then(() => {
      callback();
    });
  })

我很好奇,然后假设该电话号码用作该用户的标识符,我们将如何返回并查询Firebase身份验证表中的用户,如下面的门户网站“身份验证”视图所示

enter image description here

。查询的目的不是登录,而是查找各种用户。

1 个答案:

答案 0 :(得分:0)

您不能使用客户端SDK来查询身份验证数据库,但是可以使用Admin SDKs来查询。

这意味着您将需要在自己的服务器或Cloud Function中实现此查询。

例如,您可以编写一个Callable Cloud Function来返回特定用户的用户详细信息。

代码如下:

exports.getUserByPhone = functions.https.onCall(async (data, context) => {

    try {

        const phoneNbr = data.phoneNbr;
        const userRecord = await admin.auth().getUserByPhoneNumber(phoneNbr);

        return userRecord;

    } catch (error) {
        // See https://firebase.google.com/docs/functions/callable#handle_errors

        // Also see here the error codes: https://firebase.google.com/docs/auth/admin/errors
        // In particular, the auth/user-not-found code is returned if there is no existing user record corresponding to the provided identifier. 
    }

});

然后您可以通过传递所需的phoneNbr的值,从前端在文档中here中说明此Cloud Function。