Firebase中基于电话号码的身份验证(带数字)

时间:2016-05-26 05:55:13

标签: firebase firebase-authentication twitter-digits

我对新的Firebase和auth选项印象深刻。但是,如果我想创建自己的userID-password系统来创建用户呢?例如,我使用他的电话号码(使用Fabric的数字之类的东西)对用户进行身份验证,并使用它来创建用户帐户。现在,我如何在新的Google火力群中做到这一点?或者它甚至可行吗?

4 个答案:

答案 0 :(得分:3)

现在无法直接完成,但您可以使用Digits验证用户,将安全标头发送到您开发的后端Web服务,您可以使用电子邮件phone_number@yourdomain.com创建电子邮件/密码用户密码是您随机创建的字符串,并使用firebase自定义身份验证为最终用户提供令牌重新认证,所有这些似乎都是最终用户的电话身份验证,他甚至会知道他正在使用电子邮件/密码auth登录

答案 1 :(得分:2)

我使用了一种更简单的电话号码认证方法,通过编写我的登录功能来接受手机号码作为输入。

然后在login.ts函数中将一个公共域名附加到mobileno的末尾 - (每次调用身份验证方法时)

9xxx9@mobileappdomain.com

您不需要使用第三方Web服务,甚至Firebase中的自定义身份验证方法也不需要。

只需在Firebase中使用标准电子邮件/密码身份验证,只需更改代码,方法是将电子邮件域附加到手机号码并在代码中处理。

login() {
    this.login.mobileno = this.login.mobileno + '@appdomain.com';

       this.auth.signin(this.login)
           .then((data) => {

                ...............
                 ............................
             }  


}

答案 2 :(得分:0)

Firebase现在支持电话号码验证。

https://firebase.google.com/docs/auth/ios/phone-auth

答案 3 :(得分:0)

现在可以在firebase中使用手机验证。这是使用Firebase进行手机验证的代码:

EditText phoneNum,Code;   // two edit text one for enter phone number other for enter OTP code
Button sent_,Verify;    // sent_ button to request for verification and verify is for to verify code
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private FirebaseAuth mAuth;
private String mVerificationId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phone_number_auth);

    phoneNum =(EditText) findViewById(R.id.fn_num);
    Code =(EditText) findViewById(R.id.code);

    sent_ =(Button)findViewById(R.id.sent_nu);
    Verify =(Button)findViewById(R.id.verify);

    callback_verificvation();

    mAuth = FirebaseAuth.getInstance();
    sent_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String num=phoneNum.getText().toString();
            startPhoneNumberVerification(num);    // call function for receive OTP 6 digit code
        }
    });
    Verify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code=Code.getText().toString();
            verifyPhoneNumberWithCode(mVerificationId,code);            //call function for verify code 

        }
    });
}

private void startPhoneNumberVerification(String phoneNumber) {
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]


}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information

                        FirebaseUser user = task.getResult().getUser();
                        Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show();
                        // [START_EXCLUDE]

                        // [END_EXCLUDE]
                    } else {
                        // Sign in failed, display a message and update the UI

                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                            // [START_EXCLUDE silent]

                            // [END_EXCLUDE]
                        }
                        // [START_EXCLUDE silent]
                        // Update UI

                        // [END_EXCLUDE]
                    }
                }
            });
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
    // [START verify_with_code]
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    // [END verify_with_code]
    signInWithPhoneAuthCredential(credential);
}


private void callback_verificvation() {

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verificaiton without
            //     user action.
            // [START_EXCLUDE silent]
            // [END_EXCLUDE]

            // [START_EXCLUDE silent]
            // Update the UI and attempt sign in with the phone credential
            // [END_EXCLUDE]
            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            // [START_EXCLUDE silent]
            // [END_EXCLUDE]

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // [START_EXCLUDE]

                // [END_EXCLUDE]
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // [START_EXCLUDE]

                // [END_EXCLUDE]
            }

            // Show a message and update the UI
            // [START_EXCLUDE]

            // [END_EXCLUDE]
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.


            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;

            // [START_EXCLUDE]
            // Update UI

            // [END_EXCLUDE]
        }
    };