【问题标题】:Phone number based auth in Firebase (with Digits)Firebase 中基于电话号码的身份验证(带数字)
【发布时间】:2016-05-26 05:55:13
【问题描述】:

我对其中的新 Firebase 和身份验证选项印象深刻。但是,如果我想创建自己的用户 ID 密码系统来创建用户怎么办?例如,我使用他的电话号码验证用户(使用类似 Fabric 的数字)并使用它来创建用户帐户。现在,我怎样才能在新的 Google 火力基地中做到这一点?或者它甚至是可行的?

【问题讨论】:

标签: firebase firebase-authentication twitter-digits


【解决方案1】:

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

【讨论】:

  • 好答案,愿意分享一个示例策略,比如后端使用 web 和 node.js?
  • 由于firebase升级你不能使用上面描述的电子邮件方法,主要是因为他们改变了createUser的工作方式,但你实际上可以创建一个nodejs后端(例如express js)设置服务器原样在 firebase 文档中描述并使用与 firebase 自定义令牌混合的数字身份验证将 UID 与电话号码相关联
【解决方案2】:

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

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

9xxx9@mobileappdomain.com

您不需要为此使用第 3 方网络服务,甚至不需要使用 Firebase 中的自定义身份验证方法。

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

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

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

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


}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 给出一些步骤或一些基本的例子来确保你给出了一个好的答案,不要只是发布链接。问候!
    【解决方案4】:

    现在可以在 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]
            }
        };
    

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多