android - 无需验证验证码即可登录成功
问题描述
我正在通过firebase进行用户注册,就像通过电话号码注册whatsapp一样,我想要验证码来验证用户电话号码,然后验证收到的代码。因为我收到了验证码但是没有输入验证码也没有验证验证码就成功登录了。
String phoneNumber = InputPhoneNumber.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
Toast.makeText(MainActivity.this, "please enter your phone number first...", Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle("Phone Verfication");
loadingBar.setMessage("please wait, while we are authenticating your phone...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
MainActivity.this, // Activity (for callback binding)
callbacks); // OnVerificationStateChangedCallbacks
}
}
});
VerifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendVerificationCodeButton.setVisibility(View.INVISIBLE);
InputPhoneNumber.setVisibility(View.INVISIBLE);
String verificationCode = InputVerifcationCode.getText().toString();
if(TextUtils.isEmpty(verificationCode))
{
Toast.makeText(MainActivity.this, "please write verification code first...", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Verfication Code");
loadingBar.setMessage("please wait, while we are verifying verification code...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId, verificationCode);
signInWithPhoneAuthCredential(credential);
}
}
});
callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "Invalid phone number please enter correct number with your country code...", Toast.LENGTH_SHORT).show();
SendVerificationCodeButton.setVisibility(View.VISIBLE);
InputPhoneNumber.setVisibility(View.VISIBLE);
VerifyButton.setVisibility(View.INVISIBLE);
InputVerifcationCode.setVisibility(View.INVISIBLE);
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
mResendToken = token;
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "code has been sent, please check and verify", Toast.LENGTH_SHORT).show();
SendVerificationCodeButton.setVisibility(View.INVISIBLE);
InputPhoneNumber.setVisibility(View.INVISIBLE);
VerifyButton.setVisibility(View.VISIBLE);
InputVerifcationCode.setVisibility(View.VISIBLE);
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String currentUserId = mAuth.getCurrentUser().getUid();
RootRef.child("User").child(currentUserId).setValue("");
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "Congratulations, you are logged in successfully", Toast.LENGTH_SHORT).show();
} else {
String message = task.getException().toString();
Toast.makeText(MainActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
解决方案
推荐阅读
- php - Woocommerce 新用户注册后更新用户元数据
- javascript - 承诺“记住”先前执行的值
- powershell - -File 参数的参数“install-sshed.ps1”不存在| 电源外壳
- node.js - 错误:未指定默认引擎且未提供扩展名(ExpressJs 和 nunjucks)
- python - isinstance(list, object) 和 issubclass(list, object) 都返回 True
- javascript - getDerivedStateFromProps 没有在我的表单上显示任何更改
- javascript - 编写一个 JavaScript 类,其中一个方法可以获取和设置属性是不好的做法吗?
- python - 创建函数时出现语法错误,我不确定我做错了什么
- python - 如何使用 Python 查找和替换带有 Unicode 编码的 *.txt 文件中的字符串?
- mysql - MYSQL REGEX 搜索许多没有顺序条件的单词