我正在创建一个Android应用,以通过使用来自火力基地的手机号码OTP验证来登录用户 This is how the database looks like
这是登录活动,我将“ mobile”值传递给OTP键入活动(此处为LoginActivity.java)
CardView card_view = findViewById(R.id.cardView);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = editTextMobile.getText().toString().trim();
if(mobile.isEmpty() || mobile.length() < 10){
editTextMobile.setError("Enter a valid mobile");
editTextMobile.requestFocus();
return;
}
if(mobile.length()>10)
{
editTextMobile.setError("Enter a valid mobile");
editTextMobile.requestFocus();
return;
}
Intent intent = new Intent(LoginActivity.this, VerifyPhoneActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
}
});
这是用户输入OTP(VerifyPhoneActivity.java)的地方。如果OTP正确,并且数据库中不存在手机号码,则用户可以开始活动
public class VerifyPhoneActivity extends AppCompatActivity {
//These are the objects needed
//It is the verification id that will be sent to the user
private String mVerificationId;
DatabaseReference mDatabase;
//The edittext to input the code
private EditText editTextCode;
//firebase auth object
private FirebaseAuth mAuth;
String code;
String mobile;
String codesent;
public FirebaseAuth.AuthStateListener authListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_phone);
mAuth= FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
sendVerificationCode();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifySignInCode();
}
});
}
private void verifySignInCode()
{
final EditText fd = (EditText) findViewById(R.id.et1);
String value= fd.getText().toString();
final EditText sd = (EditText) findViewById(R.id.et2);
String value1= sd.getText().toString();
// int finalValue1=Integer.parseInt(value1);
EditText td = (EditText) findViewById(R.id.et3);
String value2= td.getText().toString();
EditText fod = (EditText) findViewById(R.id.et4);
String value3= fod.getText().toString();
EditText fid = (EditText) findViewById(R.id.et5);
String value4= fid.getText().toString();
EditText sid = (EditText) findViewById(R.id.et6);
String value5= sid.getText().toString();
code = value + value1+value2+value3+value4+value5;
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, code);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
FirebaseUser userid = FirebaseAuth.getInstance().getCurrentUser();
final String uid = userid.getUid();
ref.child("Users");
ref.child(mobile).child(uid);
Log.d("myTag", mobile);
Log.d("myTag", uid);
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
} else {
Intent intent = new Intent(VerifyPhoneActivity.this, Signup.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
// Intent myIntent = new Intent(VerifyPhoneActivity.this, Signup.class);
// startActivity(myIntent);
Toast.makeText(getApplicationContext(), "account not exist",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
Toast.makeText(getApplicationContext(), "login faild",
Toast.LENGTH_LONG).show();
// Sign in failed, display a message and update the UI
// Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(getApplicationContext(), "OTP was wrong",
Toast.LENGTH_LONG).show();
}
}
}
});
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(getApplicationContext(), "verification completed",
Toast.LENGTH_LONG).show();
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(getApplicationContext(), "sending faild"+e,
Toast.LENGTH_LONG).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
// super.onCodeSent(s, forceResendingToken);
Toast.makeText(getApplicationContext(), "sent",
Toast.LENGTH_LONG).show();
codesent=s;
}
};
private void sendVerificationCode() {
Intent intent = getIntent();
mobile = intent.getStringExtra("mobile");
Toast.makeText(getApplicationContext(), mobile,
Toast.LENGTH_LONG).show();
String phonenumber= "+91"+mobile;
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phonenumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
}
最后是Signup.java
public class Signup extends Activity {
String name;
String address;
String pincode;
String city;
String mobile;
DatabaseReference mDatabase;
FirebaseUser userid = FirebaseAuth.getInstance().getCurrentUser();
final String uid = userid.getUid();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Intent intent = getIntent();
mobile = intent.getStringExtra("mobile");
findViewById(R.id.signupbutton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), mobile,
Toast.LENGTH_LONG).show();
Log.d("myTag", mobile);
Log.d("myTag", uid);
EditText nameedittext = (EditText) findViewById(R.id.nametextview);
name= nameedittext.getText().toString();
EditText addressedittext = (EditText) findViewById(R.id.addresstextview);
address= addressedittext.getText().toString();
EditText pincodeedittext = (EditText) findViewById(R.id.picodetextview);
pincode= pincodeedittext.getText().toString();
EditText cityedittext = (EditText) findViewById(R.id.citytextview);
city= cityedittext.getText().toString();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref = ref.child("Users");
ref = ref.child(mobile).child(uid);
Map<String, String> userData = new HashMap<String, String>();
userData.put("Name", name);
userData.put("Address", address);
userData.put("Pin", pincode);
userData.put("City", city);
ref.setValue(userData);
}
});
}
}
我认为这是检查verifyphoneactivity.java中数据是否已存在的问题,请帮助我
答案 0 :(得分:0)
在问题中显示的代码中,以下几点引人注目:
创建新用户时已经登录了Signup
活动?这意味着在进行注册过程时,将使用先前的userId来注册用户,而不是新用户。
您的代码同时使用"Users"
和"users"
-Firebase区分大小写。
您的电话号码表格处理方式可能更智能,并支持区号(例如+ 91xxxxxxxxx)。这将有利于扩展到新区域,而不仅仅是检查10个数字。
以上代码无法处理验证代码的自动检索。 (docs)
注释1:考虑使用电话号码作为用户ID索引/地图。与用户的联系方式相比,您通过用户ID引用用户的频率更高,这将使您免于不断地通过应用传递用户。
{
"PhoneNumbers": {
"1234555888": "userId1",
"1234555888": "userId2",
},
"Users": {
"userId1": {
...
},
"userId2": {
...
},
...
}
}
注释2:密码存储不安全,应散列/输入到计算中,而不是以纯文本格式存储。