Android firebase创建帐户

时间:2017-06-24 17:07:02

标签: android firebase firebase-authentication

如果我想检查帐户是否已创建,我不知道如何编写if语句。 这是我的代码。 我不知道如何判断它是否存在。 请帮我解决一下。

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();
        }

我不知道如何输入此代码。

        else if(mRef==null)
        {
            Toast.makeText(getApplicationContext(),"account is existed",Toast.LENGTH_LONG).show();
        }
        else{
            mRef.child("Account").child(value).setValue(mvalue);
        }

}

}

1 个答案:

答案 0 :(得分:0)

要检查用户是否经过身份验证,即它位于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());
                    }
                }
            });
}

这应该可以解决问题。