通过Firebase登录无法正常工作,为什么?

时间:2019-01-16 08:34:07

标签: firebase android-studio firebase-authentication

我将我的应用程序连接到了Firebase,然后成功连接,然后我进行了编码,我的代码可用于注册,并且用户成功进入主屏幕并从该屏幕注销,但是我看不到在Firebase控制台中添加的任何用户。这就是为什么当我尝试再次登录时会给我消息登录错误的原因。 帮助我解决问题,并附上我的登录和注册代码

登录:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editTextEmail,editTextPassword;
    private TextView linkSignup;
    private Button buttonLogin;
    FirebaseAuth firebaseAuth;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initObjects();
        if(firebaseAuth.getCurrentUser()!=null){
            finish();
            startActivity(new Intent(this,UserHomeActivity.class));
        }
        initViews();
        initListeners();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.buttonLogin:
                userLogin();
                break;
            case R.id.linkSignup:
                finish();
                Intent intent=new Intent(this,SignupActivity.class);
                startActivity(intent);
                break;
        }

    }



    public void initViews(){

        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextEmail=(EditText)findViewById(R.id.editTextEmail);
        linkSignup=(TextView)findViewById(R.id.linkSignup);
        buttonLogin=(Button)findViewById(R.id.buttonLogin);
    }

    public void initListeners(){
        buttonLogin.setOnClickListener(this);
        linkSignup.setOnClickListener(this);
    }

    public void initObjects(){
        progressDialog=new ProgressDialog(this);
        firebaseAuth= FirebaseAuth.getInstance();
    }

    public void userLogin(){
        String email=editTextEmail.getText().toString().trim();
        String password=editTextPassword.getText().toString().trim();

        if (TextUtils.isEmpty(email)){
            Toast.makeText(this,"Please Enter your Email",Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please Enter your Password",Toast.LENGTH_SHORT).show();
            return;
        }

        progressDialog.setMessage("Logging in...");
        progressDialog.show();

        firebaseAuth.signInWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        progressDialog.dismiss();
                        if (task.isSuccessful()){
                            finish();
                            Intent intent=new Intent(getApplicationContext(),UserInfoActivity.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(getApplicationContext(),"Login error , please try again",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }


}

注册:

public class SignupActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editTextEmail,editTextPassword;
    private TextView linkLogin;
    private Button buttonSignup;
    private String email, password, name, rollNumber;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;
    private DatabaseReference databaseReference;

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

        initObjects();
        if(firebaseAuth.getCurrentUser()!=null){
            finish();
            startActivity(new Intent(this,UserHomeActivity.class));
        }
        initViews();
        initListeners();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonSignup:
                registerUser();
                break;
            case R.id.linkLogin:
                Intent intent=new Intent(this,LoginActivity.class);
                startActivity(intent);
                break;
        }

    }


    public void initViews(){

        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextEmail=(EditText)findViewById(R.id.editTextEmail);
        linkLogin=(TextView)findViewById(R.id.linkLogin);
        buttonSignup=(Button)findViewById(R.id.buttonSignup);
    }

    public void initListeners(){
        buttonSignup.setOnClickListener(this);
        linkLogin.setOnClickListener(this);

    }

    public void registerUser(){
        email=editTextEmail.getText().toString().trim();
        password=editTextPassword.getText().toString().trim();


        if (TextUtils.isEmpty(email)){
            Toast.makeText(this,"Please Enter your Email",Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please Enter your Password",Toast.LENGTH_SHORT).show();
            return;
        }


        progressDialog.setMessage("Registering User...");
        progressDialog.show();

        firebaseAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isComplete()){

                            Toast.makeText(getApplicationContext(),"Register Successfully",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(getApplicationContext(),UserHomeActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(getApplicationContext(),"Registering error , please try again",Toast.LENGTH_SHORT).show();
                        }
                        progressDialog.dismiss();


                    }
                });



    }

    public void initObjects(){
        progressDialog=new ProgressDialog(this);
        firebaseAuth= FirebaseAuth.getInstance();
        databaseReference=FirebaseDatabase.getInstance().getReference();
    }
}

我通过Firebase协助添加了依赖项。 我正在使用Android Studio 3.3。

0 个答案:

没有答案