【问题标题】:Check if user is authenticated for the first time with Firebase phone Authentication in Android检查用户是否首次通过 Android 中的 Firebase 手机身份验证进行身份验证
【发布时间】:2021-05-01 08:24:29
【问题描述】:

我需要使用 Firebase 电话身份验证检测和区分两个用户。这应该在授予进入应用家庭活动的权限之前完成。当我按照here (Stackoverflow) 的建议进行操作时,它通过使用timeStamp() 方法检测用户做得很好。答案是它的工作,但奇怪的是在发送验证码之前我需要新用户的一些数据输入。

为了发送验证码,用户提供一个直接在firebase 中验证的号码。因此我无法检查它是new user(电话号码)还是current user(电话号码)。

这是使用 TimeStamp() 方法的代码。

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential)
{
    _firebaseAuth.signInWithCredential(credential).addOnCompleteListener(Objects.requireNonNull(getActivity()), task ->
    {
        if(task.isSuccessful())
        {
            //Sign in success, update UI with the signed-in user's information.
            FirebaseUser _user = Objects.requireNonNull(task.getResult()).getUser();
            long creationTimestamp = Objects.requireNonNull(Objects.requireNonNull(_user).getMetadata()).getCreationTimestamp();
            long lastLoginTimestamp = Objects.requireNonNull(Objects.requireNonNull(_user).getMetadata()).getLastSignInTimestamp();

            if(creationTimestamp == lastLoginTimestamp)
            {
                //Create a new user with account
                setUserDataToDatabase(_user, _username, _university, _course, _year);
                sendUserToWelcome();
            }
            else
            {
                //User exists, just login
                sendUserToHome();
            }
        }
        else
        {
            FancyToast.makeText(getContext(), "Enter sent code", FancyToast.LENGTH_SHORT, FancyToast.INFO, false).show();
        }
    });
}

【问题讨论】:

    标签: android firebase-authentication


    【解决方案1】:

    经过几次研究都没有成功。我决定四处走走,我正在使用firestore database。我决定使用自动生成的document id 在新的collection 中跟踪每个用户的号码。我将集合称为 USERS,而每个 document 都有一个唯一的随机 ID。

    我得到用户的号码,并使用带有 phone_number 字段。 If 号码存在 我登录用户else 显示注册屏幕。

     _firestore.collection(USERS).whereEqualTo("phone_number", _phoneCheck).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                        {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task)
                            {
                                if(task.isSuccessful())
                                {
                                    //If task is greater than 0 means there is a presence of a phone number.
                                    if(Objects.requireNonNull(task.getResult()).size() > 0)
                                    {
                              
                                    //Here I allow user to login as usual.
                                    PhoneAuthOptions options = PhoneAuthOptions.newBuilder(_firebaseAuth).setPhoneNumber(_phone).setTimeout(60L, TimeUnit.SECONDS).setActivity(Objects.requireNonNull(getActivity())).setCallbacks(_callbacks).build();
                                    PhoneAuthProvider.verifyPhoneNumber(options);
                                    }
                                }
                                else 
                                {
                                    //Else the task is empty means there is no a presence of a phone number.
    
                                    //Check if there is a presence of registration data to bind with new user.
                                    if(_registrationData != null)
                                    {
                 
                                        //I login user with the new data and save the information into the firestore plus the phone number.
                                        PhoneAuthOptions options = PhoneAuthOptions.newBuilder(_firebaseAuth).setPhoneNumber(_phone).setTimeout(60L, TimeUnit.SECONDS).setActivity(Objects.requireNonNull(getActivity())).setCallbacks(_callbacks).build();
                                        PhoneAuthProvider.verifyPhoneNumber(options);
                                        userInputs();
                                    }
                                    else
                                    {
                                        //Display a welcome a screen to register an account.
                                        FancyToast.makeText(getContext(), "Welcome! Open an account", FancyToast.LENGTH_SHORT, FancyToast.INFO, false).show();
                                    }
                                }
                            }
                        }
                    });
    

    允许未经身份验证的用户拥有进入数据库的权限是非常危险的。因此,我实现了一个规则,允许未经身份验证的用户只读。

    match /USERS/{document=**}
    {
        allow read: if true;
    }
    

    虽然这仍然是有风险的,但任何规则建议我都将是毕业和可欣赏的。

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2019-12-29
      相关资源
      最近更新 更多