gpt4 book ai didi

java - 如何根据我的实时数据库以及 OTP 验证来验证用户?

转载 作者:行者123 更新时间:2023-11-30 02:13:40 25 4
gpt4 key购买 nike

我在 Android Studio 中创建了一个 Android 应用,并将其与 Firebase 实时数据库链接。我已使用 Firebase 的电话身份验证和通知服务将 OTP 发送到 CUG 电话号码。然后验证一下(代码如下)。

public class PhoneLogin extends AppCompatActivity {

private static final String TAG = "PhoneLogin";
private boolean mVerificationInProgress = false;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private FirebaseAuth mAuth;
TextView t1,t2;
ImageView i1;
EditText e1,e2;
Button b1,b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_login);

e1 = (EditText) findViewById(R.id.Phonenoedittext); //Enter Phone no.
b1 = (Button) findViewById(R.id.PhoneVerify); //Send OTP button
t1 = (TextView)findViewById(R.id.textView2Phone); //Telling user to enter phone no.
i1 = (ImageView)findViewById(R.id.imageView2Phone); //Phone icon
e2 = (EditText) findViewById(R.id.OTPeditText); //Enter OTP
b2 = (Button)findViewById(R.id.OTPVERIFY); //Verify OTP button
t2 = (TextView)findViewById(R.id.textViewVerified); //Telling user to enter otp
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// Log.d(TAG, "onVerificationCompleted:" + credential);
mVerificationInProgress = false;
Toast.makeText(PhoneLogin.this,"Verification Complete",Toast.LENGTH_SHORT).show();
signInWithPhoneAuthCredential(credential);
}

@Override
public void onVerificationFailed(FirebaseException e) {
// Log.w(TAG, "onVerificationFailed", e);
Toast.makeText(PhoneLogin.this,"Verification Failed",Toast.LENGTH_SHORT).show();
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
Toast.makeText(PhoneLogin.this,"InValid Phone Number",Toast.LENGTH_SHORT).show();
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
}
}

@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// Log.d(TAG, "onCodeSent:" + verificationId);
Toast.makeText(PhoneLogin.this,"Verification code has been sent",Toast.LENGTH_SHORT).show();
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
e1.setVisibility(View.GONE);
b1.setVisibility(View.GONE);
t1.setVisibility(View.GONE);
i1.setVisibility(View.GONE);
t2.setVisibility(View.VISIBLE);
e2.setVisibility(View.VISIBLE);
b2.setVisibility(View.VISIBLE);
// ...
}
};

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
e1.getText().toString(),
60,
java.util.concurrent.TimeUnit.SECONDS,
PhoneLogin.this,
mCallbacks);
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, e2.getText().toString());
// [END verify_with_code]
signInWithPhoneAuthCredential(credential);
}
});


}

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");
startActivity(new Intent(PhoneLogin.this,NavigationDrawer.class));
Toast.makeText(PhoneLogin.this,"Verification Done",Toast.LENGTH_SHORT).show();
// ...
} else {
// Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(PhoneLogin.this,"Invalid Verification",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}

现在我想添加另一个验证,以验证输入的 CUG 编号。存在于我的数据库中,然后只应进行 OTP 身份验证。我的数据库如下所示:

My database

访问该数据库的代码可能是

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference phoneNumberRef =
rootRef.child("Employees").child(PhoneNumberenteredByUser);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//do something
} else {
//do something else
}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
phoneNumberRef.addListenerForSingleValueEvent(eventListener);

此外,当我看到 Firebase 实时数据库的规则时,我注意到它不应该公开,但如果我需要将其保持私有(private),那么应该首先对用户进行身份验证,所以我需要首先对用户进行身份验证通过 OTP,然后检查用户 CUG 号是否。我的数据库中存在吗?

编辑后的代码可验证任何号码。即使它不在我的数据库中:

public class PhoneLogin extends AppCompatActivity {
private static final String TAG = "PhoneLogin";
private boolean mVerificationInProgress = false;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private FirebaseAuth mAuth;
TextView t1,t2;
ImageView i1;
EditText e1,e2;
Button b1,b2;

//DBA1
private DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
private DatabaseReference phoneNumberRef;
String mobno;
//DBA1 End

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_login);

e1 = (EditText) findViewById(R.id.Phonenoedittext);
b1 = (Button) findViewById(R.id.PhoneVerify);
t1 = (TextView) findViewById(R.id.textView2Phone);
i1 = (ImageView) findViewById(R.id.imageView2Phone);
e2 = (EditText) findViewById(R.id.OTPeditText);
b2 = (Button) findViewById(R.id.OTPVERIFY);
t2 = (TextView) findViewById(R.id.textViewVerified);

mobno=e1.getText().toString();

//DBA2
phoneNumberRef = rootRef.child("Employees").child(mobno);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {


@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// Log.d(TAG, "onVerificationCompleted:" + credential);
mVerificationInProgress = false;
Toast.makeText(PhoneLogin.this,"Verification Complete",Toast.LENGTH_SHORT).show();
signInWithPhoneAuthCredential(credential);
}

@Override
public void onVerificationFailed(FirebaseException e) {
// Log.w(TAG, "onVerificationFailed", e);
Toast.makeText(PhoneLogin.this,"Verification Failed",Toast.LENGTH_SHORT).show();
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
Toast.makeText(PhoneLogin.this,"InValid Phone Number",Toast.LENGTH_SHORT).show();
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
}

}

@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// Log.d(TAG, "onCodeSent:" + verificationId);
Toast.makeText(PhoneLogin.this,"Verification code has been sent",Toast.LENGTH_SHORT).show();
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
e1.setVisibility(View.GONE);
b1.setVisibility(View.GONE);
t1.setVisibility(View.GONE);
i1.setVisibility(View.GONE);
t2.setVisibility(View.VISIBLE);
e2.setVisibility(View.VISIBLE);
b2.setVisibility(View.VISIBLE);
// ...
}
};

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
e1.getText().toString(),
60,
java.util.concurrent.TimeUnit.SECONDS,
PhoneLogin.this,
mCallbacks);
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, e2.getText().toString());
// [END verify_with_code]
signInWithPhoneAuthCredential(credential);
}
});



} else {
Toast.makeText(getApplicationContext(),"Incorrect CUG",Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
}
};
phoneNumberRef.addListenerForSingleValueEvent(eventListener);
//DBA2 End


}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
startActivity(new Intent(PhoneLogin.this,NavigationDrawer.class));
Toast.makeText(PhoneLogin.this,"Verification Done",Toast.LENGTH_SHORT).show();
// Log.d(TAG, "signInWithCredential:success");
//startActivity(new Intent(PhoneLogin.this,NavigationDrawer.class));
Toast.makeText(PhoneLogin.this,"Verification Done",Toast.LENGTH_SHORT).show();
// ...
} else {
// Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(PhoneLogin.this,"Invalid Verification",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}

最佳答案

很简单,

编辑您的

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, e2.getText().toString());
// [END verify_with_code]
signInWithPhoneAuthCredential(credential);
}
});

与:

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Write your database reference and check in the database for entered mobno.
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(mobno).exists()){
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, e2.getText().toString());
// [END verify_with_code]
signInWithPhoneAuthCredential(credential);
}
else{
Toast.makeText(PhoneLogin.this,"No such CUG No. found",Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

关于java - 如何根据我的实时数据库以及 OTP 验证来验证用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49314285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com