我正在使用firebase手机身份验证。当用户使用电话号码创建帐户时,下次他创建具有相同电话号码的帐户 比我想显示一条消息说帐户已存在
答案 0 :(得分:2)
对于解决方案,
注册后,请在数据库中输入一些用户身份的条目,以便下次您可以识别用户已注册。
在实时数据库中进行OTP验证检查后,手机号码已经存在,因此它已经输入了该特定手机号码。
答案 1 :(得分:2)
我已经在我的项目中完成了这项工作并且工作正常,您可以使用此功能,您可以使用您的电话号码而不是“deviceId”。
mFirebaseDatabaseRefrence.orderByChild("deviceId").equalTo(deviceId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
Log.d(TAG, "Datasnap Shots: " + dataSnapshot.getValue());
/* if alredy exist and check for first time, second time isExist=true*/
if (!isExist) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
UserbasicInfo user = userSnapshot.getValue(UserbasicInfo.class);
Toast.makeText(UserInfoActivity.this, "User already exist...!", Toast.LENGTH_SHORT).show();
}
}
isExist = true;
} else {
isExist = false;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/*if not exist add data to firebase*/
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "isExist: " + isExist);
if (!isExist) {
addDataToDB(false);
} else {
addDataToDB(true);
}
}
};
new Handler().postDelayed(runnable, 5000);
答案 2 :(得分:2)
为了检测该电话号码是否已用于帐户注册,您不仅可以依赖默认的身份验证表。但是还必须使用Firebase数据库来创建虚拟用户表以进行检查。
例如,您可以创建一个json树,将实时数据库中的用户数据保存为以下结构:
您的代码应该类似于:
在成功登录/用户注册的代码:
DatabaseRef userRef = FirebaseDatabase.getInstance.getRef("users");
userRef.orderByChild("telNum").equalTo(phoneNumber).addListenerForSingleValueEvent(new ValueEventListener() {
if (dataSnapshot.getValue() != null){
//it means user already registered
//Add code to show your prompt
showPrompt();
}else{
//It is new users
//write an entry to your user table
//writeUserEntryToDB();
}
}
答案 3 :(得分:2)
onTask
结果检查FirebaseAuthUserCollisionException
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(Signup.this, "User already exist.", Toast.LENGTH_SHORT).show();
}
答案 4 :(得分:0)
我这样处理过。 Firebase会因为在身份验证和其他无效操作时遇到错误而抛出一些异常,我们只需处理它们并通知用户。
示例代码:
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
//do something on success
} else {
//handle the error here
showInfoToUser(task)
}
}
}
});
}
并通过检查此处的例外来检查我们得到的错误
private void showInfoToUser(Task<AuthResult> task) {
//here manage the exceptions and show relevant information to user
hideProgressDialog();
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
showSnackBar(getString(R.string.user_already_exist_msg));
} else if (task.getException() instanceof FirebaseAuthWeakPasswordException) {
showSnackBar(task.getException().getMessage());
} else if (task.getException() instanceOf FirebaseAuthInvalidCredentialsException){
//invalid phone /otp
showSnackBar(task.getException().getMessage());
}
else {
showSnackBar(getString(R.string.error_common));
}
}
答案 5 :(得分:0)
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users");
userRef
.orderByChild("phonenumber")
.equalTo(mMobile.getText().toString())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
//it means user already registered
//Add code to show your prompt
OpenErrorAlrt("Mobile Number already registed");
} else {
Intent intent = new Intent(getApplicationContext(), OTPVerifyActivity.class);
intent.putExtra("phonenumber", mMobile.getText().toString());
startActivity(intent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 6 :(得分:0)
这是检查用户是否已经在Firebase中注册的非常简单的方法。
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
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
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
if(task.getResult().getAdditionalUserInfo().isNewUser()){
register(user);
}else{
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
Toast.makeText(LoginActivity.this, "welcome"+user.getDisplayName(), Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(LoginActivity.this, "signin Failed", Toast.LENGTH_SHORT).show();
}
// ...
}
});