android - Firebase 电话验证
问题描述
您好,我是 android studio 开发的新手,我遵循 youtube 教程。我按照代码进行操作,但是当我运行该应用程序时,我的电话验证失败。我来自菲律宾,目前在我的电话号码上使用 +63 代码,但未能通过 firebase 数据库进行身份验证。请帮忙,这里是代码...
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class PhoneLoginActivity extends AppCompatActivity {
private Button SendVerificationCodeButton, VerifyButton;
private EditText InputPhoneNumber, InputVerificationCode;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks;
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_login);
mAuth = FirebaseAuth.getInstance();
SendVerificationCodeButton = (Button) findViewById(R.id.send_ver_code_button);
VerifyButton = (Button) findViewById(R.id.verify_button);
InputPhoneNumber = (EditText) findViewById(R.id.phone_number_input);
InputVerificationCode = (EditText) findViewById(R.id.verification_code_input);
loadingBar = new ProgressDialog(this);
SendVerificationCodeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = InputPhoneNumber.getText().toString();
if(TextUtils.isEmpty(phoneNumber)){
Toast.makeText(PhoneLoginActivity.this, "Please enter your phone number first...", Toast.LENGTH_SHORT).show();
}
else{
loadingBar.setTitle("Phone Verification");
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
PhoneLoginActivity.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 = InputVerificationCode.getText().toString();
if(TextUtils.isEmpty(verificationCode)){
Toast.makeText(PhoneLoginActivity.this, "Please write the verification code first...", Toast.LENGTH_SHORT).show();
}
else{
loadingBar.setTitle("Verification Code");
loadingBar.setMessage("Please wait while verifying code...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, verificationCode);
signInWithPhoneAuthCredential(credential);
}
}
});
callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
loadingBar.dismiss();
Toast.makeText(PhoneLoginActivity.this, "Invalid phone number, please enter correct phone number with your country code" , Toast.LENGTH_SHORT).show();
SendVerificationCodeButton.setVisibility(View.VISIBLE);
InputPhoneNumber.setVisibility(View.VISIBLE);
VerifyButton.setVisibility(View.INVISIBLE);
InputVerificationCode.setVisibility(View.INVISIBLE);
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
mResendToken = token;
loadingBar.dismiss();
Toast.makeText(PhoneLoginActivity.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);
InputVerificationCode.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()) {
loadingBar.dismiss();
Toast.makeText(PhoneLoginActivity.this, "Congratulations, you're logged in succcessfully...", Toast.LENGTH_SHORT).show();
SendUserToMainActivity();
} else {
String message = task.getException().toString();
Toast.makeText(PhoneLoginActivity.this, "Error :" +message, Toast.LENGTH_SHORT).show();
}
}
});
}
private void SendUserToMainActivity() {
Intent mainIntent = new Intent(PhoneLoginActivity.this,MainActivity.class
);
startActivity(mainIntent);
finish();
}
}
解决方案
推荐阅读
- c# - 使用 Linq 对实体执行 convert(NVARCHAR, ValidDate.Date, 102)
- python - 在django中过滤多对多字段的外键?
- arrays - VBA Excel - 从二维数组创建饼图
- javascript - 所选选项动态禁用具有相同值的复选框
- python-3.x - 有没有办法使用 bs4 获取具有特定属性的标签中的文本?
- asp.net-core - 我是为 JWT 使用 [Authorize] 标头还是在公共端点内部进行检查?
- django - Django 表单 imageupload 给出空 dic
- continuous-integration - 赛普拉斯无法从 GitHub 机密中读取密码,但能够获取记录密钥
- android - 无法覆盖 androidx.fragment.app.Dialog 的 onCreateDialog 方法
- security - 防火墙日志查询