android - 检查用户是否首次通过 Android 中的 Firebase 手机身份验证进行身份验证
问题描述
我需要使用Firebase 电话身份验证来检测和区分两个用户。这应该在授予进入应用程序的家庭活动的权限之前完成。当我按照这里 (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();
}
});
}
解决方案
经过多次研究没有成功。我决定四处走走,我正在使用firestore database
. 我决定collection
使用自动生成的新号码跟踪每个用户的号码document id
。我调用了这个集合USERS
,而每个集合document
都有一个唯一的随机 ID。
我得到用户的号码,并使用带有字段的方法检查是否有任何注册用户在USERS
's 集合中拥有该号码。该号码存在我登录用户显示注册屏幕。whereEqualTo()
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;
}
虽然这仍然是有风险的,但任何规则建议我都将是毕业和可欣赏的。
推荐阅读
- arrays - 如何将自定义输入组件中的输入值推送到父组件中的数组中?
- reactjs - how to handle multiple popover (material ui)
- r - 建立一个带有奇怪结构的列表(?)
- java - 使用不同的定位器定位 WebElement (NoSuchElementException)
- vagrant - default: SSH auth method: private key default: Warning: Authentication failure. Retrying
- javascript - Node.js JIMP restarting app after file was saved
- splunk - How to setup splunk summary index?
- java - Java: Unable to understand for loop behaviour
- java - TOMCAT 9 +Java 8 升级问题:方法 URLDecode(String) 未定义类型 RequestUtil
- php - I want sum of array specific array item's value until not reach at the end?