Firebase:注册用户后如何进行电话号码身份验证? [英] Firebase: How to do a phone number authentication after the user has been registered?

查看:60
本文介绍了Firebase:注册用户后如何进行电话号码身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我知道我可以使用电子邮件验证或电话号码验证,但是我要做的是在用户注册或登录后进行电话号码验证.如何连接这两种身份验证方法.最后,Firebase中是否有一个功能可以检查用户是否通过电话号码验证?谢谢.

So I know I can use email verification, or phone number verification, but what I want to do is a phone number verification after the user has registered or logged in. How do you connect this these two authentication methods. Finally, is there a function in Firebase to check if the user is verified by phone number or not? Thank you.

推荐答案

即使用户通过了身份验证,您仍然可以使用firebase提供的APi来验证号码.根据 docs ,进行身份验证仅当用户收到确认码并生成 PhoneAuthCredential 时.如果您只想验证电话,则只需提供对回调 onVerificationCompleted 的自定义响应即可.

You can still use the APi provided by firebase to verify the number even if the user is authenticated. According to the docs , the authentication happens only when the user receives the confirmation code and generates a PhoneAuthCredential. If you just want to vrify the phone you can simply provide a custom reaction to the callback onVerificationCompleted.

通常,您设置提供商:

PhoneAuthProvider.getInstance().verifyPhoneNumber(
        phoneNumber,        
        60,                 
        TimeUnit.SECONDS,   
        this,               
        mCallbacks);        

然后实现一系列回调.

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {
        //No need to authenticate again, just react to verified number
        //signInWithPhoneAuthCredential(credential);
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {

        if (e instanceof FirebaseAuthInvalidCredentialsException) {

        } else if (e instanceof FirebaseTooManyRequestsException) {

        }

    }

    @Override
    public void onCodeSent(String verificationId,
                           PhoneAuthProvider.ForceResendingToken token) {

        mVerificationId = verificationId;
        mResendToken = token;

    }
};

根据关于验证用户登录方式的第二个问题,您可以检查

According to your second question about to verify how the user is signed in you can check this answer to see how to check the firebase user authentication providers.

用户登录后,您可以通过以下方式获取其电话号码(如果有):

When a user is logged in you can get its phone number (if there is any) by calling:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String number = user.getPhoneNumber();

这篇关于Firebase:注册用户后如何进行电话号码身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆