[英]Android firebase create account
I don't know how to write the if statement if I want to check if the account had been create or not. 如果我想检查帐户是否已创建,我不知道如何编写if语句。 Here is my code.
这是我的代码。 I don't know how to judge if it is existed or not.
我不知道如何判断它是否存在。 Please help me to solve it.
请帮我解决一下。
public class MainActivity extends AppCompatActivity {
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
private Button Go;
private EditText account;
private EditText password;
private EditText pass2;
private FirebaseAuthException mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
account = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
pass2 = (EditText) findViewById(R.id.editText3);
Go = (Button) findViewById(R.id.button2);
Go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSignin();
Intent intent = new Intent();
intent.setClass(MainActivity.this,MemberCenter.class);
startActivity(intent);
}
});
}
private void startSignin(){
String value = account.getText().toString();
String mvalue = password.getText().toString();
String two = pass2.getText().toString();
if(mvalue.equals("")||value.equals(""))
{
Toast.makeText(getApplicationContext(),"cant be empty!",Toast.LENGTH_LONG).show();
}
else if(!mvalue.equals(two))
{
Toast.makeText(getApplicationContext(),"password are different",Toast.LENGTH_LONG).show();
}
I don't know how to type this code. 我不知道如何输入这段代码。
else if(mRef==null)
{
Toast.makeText(getApplicationContext(),"account is existed",Toast.LENGTH_LONG).show();
}
else{
mRef.child("Account").child(value).setValue(mvalue);
}
}
} }
To check if the user is authenticated ie it is in your list of users in Firebase Account or not just use this. 要检查用户是否经过身份验证,即它位于Firebase帐户中的用户列表中,或者只是使用此用户。
FirebaseAuth auth=FirebaseAuth.getInstance();
if(auth!=null){
//account exist
//do your work here
checkIfUserExists();
}
else {
//account does not exist.You may exit the app or do whatever you want
}
void checkIfUserExists(){
FirebaseAuth mFirebaseAuth=FirebaseAuth.getInstance();
mFirebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//User registered successfully
}else{
Log.e("Response","Failed to create user:"+task.getException().getMessage());
}
}
});
}
This should do the trick. 这应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.