简体   繁体   中英

Firebase Authentication with Android

I'm trying to get my Android app to authenticate users (just with email/password for now) through Firebase. I have a Login, Register, and Main Activity (Home page).

public class Register extends AppCompatActivity {

private EditText txtEmailAddress, txtPassword;
private FirebaseAuth firebaseAuth;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    txtEmailAddress = (EditText)findViewById(R.id.reg_email);
    txtPassword = (EditText)findViewById(R.id.reg_password);
    firebaseAuth = FirebaseAuth.getInstance();

}

public void btnRegistrationUser_Click(View v) {
    final ProgressDialog progressDialog = ProgressDialog.show(Register.this, "Please wait", "Processing",true);
    firebaseAuth.createUserWithEmailAndPassword(txtEmailAddress.getText().toString(),txtPassword.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            progressDialog.dismiss();

            if(task.isSuccessful()){
                Toast.makeText(Register.this,"Registration successful",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Register.this,LoginActivity.class);
                startActivity(intent);

            }else{
                Log.e("ERROR",task.getException().toString());
                Toast.makeText(Register.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
            }
            progressDialog.dismiss();
        }
    });
}

This is my register.java. I can run the application fine, but after I enter the info and click the button, I get an endless ProgressDialog just spinning and no update to Firebase.

I've followed different tutorials and looked for answers, but can't seem to find any fix. I have my manifest page allow internet permission, I don't believe I have any issues with the gradle (the app runs fine minus not allowing me to get to the home page now (lol), but no errors), and I have google-services.json pasted in my app.

If it helps, the code below is my login.java. Thanks for any help out there!

public class LoginActivity extends AppCompatActivity {

private EditText txtEmailLogin;
private EditText txtPassword;

private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;


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

    txtEmailLogin = (EditText) findViewById(R.id.editTextEmail);
    txtPassword = (EditText)findViewById(R.id.editTextPassword);
    firebaseAuth = FirebaseAuth.getInstance();
}

public void btnRegistration_Click(View v){
    Intent registration = new Intent(LoginActivity.this,Register.class);
    startActivity(registration);
}

public void btnUserLogin_Click(View v){
    final ProgressDialog progressDialog = ProgressDialog.show(LoginActivity.this,"Please wait","Processing",true);

    firebaseAuth.signInWithEmailAndPassword(txtEmailLogin.getText().toString(),txtPassword.getText().toString())
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();

                    if(task.isSuccessful()){
                        Toast.makeText(LoginActivity.this,"Login successful",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent (LoginActivity.this,MainActivity.class);
                        startActivity(intent);

                    }else{
                        Log.e("ERROR",task.getException().toString());
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
                    }
                }
            });
}
@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

If you use android Studio click tools and configuration is a snap your problem seems to be registering onauthstatelistner

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM