【问题标题】:How to Implement firebase authentication fields with database fields with android studio app?如何使用 android studio 应用程序实现带有数据库字段的 firebase 身份验证字段?
【发布时间】:2019-03-03 19:56:35
【问题描述】:

我知道如何将 Firebase 与电话身份验证或电子邮件一起使用,但我必须同时使用 - 我的应用程序中的电话号码和电子邮件号码要存储在数据库中。 可能吗 ? 如果是,请在下面提供帮助。 我在firebase上有一个带有电话otp的身份验证字段。 如何将身份验证表中的电话号码与其他字段一起存储到数据库表中。

【问题讨论】:

  • 嘿@BHASKAR 确实通过单击答案旁边的 V 或勾选类型查找按钮将答案标记为正确,因为这有助于堆栈溢出类似问题的人们,我也很感激。干杯! :)

标签: android firebase firebase-realtime-database firebase-authentication


【解决方案1】:

您可以为您的用户设置这两个选项。您可以放置​​一个按钮用于使用 Google ID 登录,另一个按钮用于输入他们的电话号码进行登录。

如果你不知道怎么做,你只需要添加相关按钮,在java部分,你可以使用这样的东西:

这是获取和验证 OTP,使用 Google ID 和电话号码登录的部分。

还有更多内容,例如在您应用的 Google 登录按钮的 onClickListener() 中使用 signIn() 方法,但我认为您可以自己执行此操作。

private void signIn(){
        Intent sIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(sIntent,RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);

        if(requestCode == RC_SIGN_IN){
            GoogleSignInResult result  = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if(result.isSuccess()){
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            }
            else{
                Toast.makeText(MainActivity.this,"Auth went wrong",Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount account){
        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    Log.d("Tag","SignInWithCredential: success");

                    FirebaseUser fUser = mAuth.getCurrentUser();
                    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
                    assert fUser != null;

                    // do what you want on successful signIN, like storing the email using ref
                }
                else{
                    Log.w("TAG","SignInWithCredential: failure", task.getException());
                    Toast.makeText(MainActivity.this,"Authentication failed", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

    private void GetOTP(){

        String pn = phone.getText().toString();

        if(pn.isEmpty()){
            Toast.makeText(MainActivity.this,"Phone number is required", Toast.LENGTH_SHORT).show();
        }
        else if(pn.length()<10){
            Toast.makeText(MainActivity.this,"Valid Phone number is required", Toast.LENGTH_SHORT).show();
        }
        else
            PhoneAuthProvider.getInstance().verifyPhoneNumber(pn,60, TimeUnit.SECONDS,this,mCallBacks);
    }

    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {

        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);

            otp = s;
        }
    };

    private void verifyCode(){

        String code = cd.getText().toString();
        String pH = phone.getText().toString();
        if(code.equals("") && pH.equals(""))
            Toast.makeText(MainActivity.this,"Nothing to validate", Toast.LENGTH_SHORT).show();
        else {
            try {
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otp, code);
                signInWithPhoneAuthCredential(credential);
            }
            catch (Exception e) {
                Log.i("exception",e.toString());
                Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_LONG).show();
            }

        }
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential){
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    startActivity(new Intent(MainActivity.this,Main2Activity.class));
                }
                else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        Toast.makeText(MainActivity.this, "OTP is incorrect", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

获得 emailID 或电话号码后,您可以在 Firebase 数据库中将其设为您用户的 identifier

我的意思是,您可以将该用户的所有其他详细信息保存在您获得的电话号码/电子邮件下。

在代码中看起来像这样:

//email is the email of the user and phone is the phone Number

if(email==null){
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  ref.child("users").child(phone).child("username").setValue(uName);

  // you can set other details in the same way

}

else if(phone==null){
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  ref.child("users").child(email).child("username").setValue(uName);

  // you can set other details in the same way

}

// uName is the username you want to store for the user

【讨论】:

    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多