【发布时间】:2018-06-12 10:35:39
【问题描述】:
我使用 Firebase 电话身份验证和实时数据库制作了一个 Android 应用程序。
我的要求是首先检查用户的号码。是否在我的数据库中,然后将 OTP 发送给他。
我的代码:
rootRef= FirebaseDatabase.getInstance().getReference().child("Employees").child("mobno");
//---------------1----------------
getcodeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("abcd","get code button clicked");
if (!cugET.getText().toString().trim().isEmpty() && cugValidFlag==true) {
Log.d("abcd","entered no. is valid");
enteredcugno = cugET.getText().toString().trim();
Log.d("abcd","Entered cugno: "+enteredcugno);
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("abcd","rootRef listener reached");
if(dataSnapshot.child(enteredcugno).exists()){
Log.d("abcd","cugno exists in db");
startPhoneNumberVerification(cugET.getText().toString().trim());
mVerified = false;
otpTV.setVisibility(View.VISIBLE);
otpET.setVisibility(View.VISIBLE);
otpValTV.setVisibility(View.VISIBLE);
verifycodeBtn.setVisibility(View.VISIBLE);
}
else{
Log.d("abcd","cugno doesn't exists in db");
Toast.makeText(cugLogin.this,"No such CUG No. found",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
//------------------------2--------------------
private void startPhoneNumberVerification(String phoneNumber) {
Log.d("abcd","startPhoneNumberVerification");
// [START start_phone_auth]
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91"+phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
// [END start_phone_auth]
}
//--------------3--------------------
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verificaiton without
// user action.
Log.d("abcd", "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w("abcd", "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
Log.d("abcd","verification failed cz of FirebaseAuthInvalidCredentialsException");
Toast.makeText(cugLogin.this,"Verification Failed !! Invalied verification Code",Toast.LENGTH_SHORT).show();
}
else if (e instanceof FirebaseTooManyRequestsException) {
Log.d("abcd","verification failed cz FirebaseTooManyRequestsException");
Toast.makeText(cugLogin.this,"Verification Failed !! Too many request. Try after some time. ",Toast.LENGTH_SHORT);
}
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d("abcd", "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
}
};
//----------------4---------------
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
Log.d("abcd","signInWithPhoneAuthCredential reached");
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("abcd", "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
mVerified = true;
timer.cancel();
timerTV.setVisibility(View.INVISIBLE);
cugET.setEnabled(false);
cugTV.setVisibility(View.INVISIBLE);
cugET.setVisibility(View.INVISIBLE);
getcodeBtn.setVisibility(View.INVISIBLE);
otpTV.setVisibility(View.INVISIBLE);
otpET.setVisibility(View.INVISIBLE);
otpValTV.setVisibility(View.INVISIBLE);
verifycodeBtn.setVisibility(View.INVISIBLE);
Intent intent = new Intent(cugLogin.this,Login.class);
intent.putExtra("cugnotoLogin",enteredcugno);
startActivity(intent);
Toast.makeText(cugLogin.this,"Successfully verified",Toast.LENGTH_SHORT).show();
// ...
} else {
// Sign in failed, display a message and update the UI
Log.w("abcd", "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(cugLogin.this,"Invalid OTP ! Please enter correct OTP",Toast.LENGTH_SHORT).show();
}
}
}
});
}
我看到的最后一条日志是“Entered cugno: 9876543210”
没有显示错误,我真的很困惑如何纠正它?
如果我删除 rootRef 侦听器,我的代码正在运行,但我想先检查数据库中的用户。我也给出了 rootRef 的正确路径,那为什么它不起作用?
编辑
我的代码可以正常工作,但我仍然有点困惑,我只是将 rootRef 初始化移到 getcodeBtn 侦听器中。但我已经在我的 onStart() 函数中有它,所以它也应该在那里初始化,如果有人知道它背后的原因,请告诉我。
最新编辑
现在,当我退出时,我的代码又卡在了 rootRef 监听器上,它没有被传递进去。即使在关闭应用程序后,它也不会让我进入。同样的事情再次发生。这就像一夜之间刷新了一些东西,应用程序让我登录,当我退出时,我又被卡住了。为什么会这样?
【问题讨论】:
-
您在代码中的哪个位置登录?
-
@AlexMamo 在 rootRef 监听器中,我正在启动 startPhoneNumberVerification(cugeET.getText().toString().trim());然后它转到 mCallbacks 等等。我的要求是首先检查用户是否存在于数据库中,然后只发送 OTP
-
Here 是您检查数据库中是否存在用户的方法。
-
我已经做到了,一切正常,我只是更改了注销代码,当我注销时,从那以后我无法登录,因为代码没有进入 rootRef听众
-
使用第一个代码sn-p,你根本没有登出,你只是完成一个活动。使用第二个,您将注销。
FirebaseAuth.getInstance().signout()正在帮助您做到这一点。为什么无法再次登录,这是您共享的代码无法解决的另一个问题。
标签: android firebase firebase-realtime-database firebase-authentication