0

I seem to have altered something in my code and I can't figure out what it is I need to revert.

I'm building an app that will have a MainActivity that will lead to a loginActivity. However, when the login button is pressed the app is now skipping that and going to a MapActivity that should only be visible to logged in users.

I've checked my MainActivity code and I don't think the problem is there, I think it's the LoginActivity code:

public class ResponderLoginActivity extends AppCompatActivity {
    private EditText mEmail, mPassword;
    private Button mLogin, mRegistration;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null) {
                    Intent intent = new Intent(ResponderLoginActivity.this, ResponderMapActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }
            }
        };

        mEmail = (EditText) findViewById(R.id.email);
        mPassword = (EditText) findViewById(R.id.password);

        mLogin = (Button) findViewById(R.id.login);
        mRegistration = (Button) findViewById(R.id.registration);

        mRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String email = mEmail.getText().toString();
                final String password = mPassword.getText().toString();
                mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(ResponderLoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(ResponderLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
                        } else {
                            String user_id = mAuth.getCurrentUser().getUid();
                            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Responders").child(user_id);
                            current_user_db.setValue(true);
                        }
                    }
                });
            }
        });

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String email = mEmail.getText().toString();
                final String password = mPassword.getText().toString();
                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(ResponderLoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(ResponderLoginActivity.this, "Sign in error", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(firebaseAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mAuth.removeAuthStateListener(firebaseAuthListener);
    }
}
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
  • Can't see anything obvious. Have you tried debugging? Also, reverting from source control and redoing changes until it happens? – Squirrelkiller Jul 31 '18 at 20:11
  • You are getting `user != null` in `onAuthStateChanged ` thats why when you click in Login button in `MainActivity` it is navigating you to `ResponderLoginActivity` and in `onCreate` of `ResponderLoginActivity`, `FirebaseUser` object is already have some user value other than `null` so it is navigating you to `MapActivity` – Varad Mondkar Jul 31 '18 at 20:17
  • @Squirrelkiller Yeah, tried debugging but as I'm only an amateur, I couldn't identify the reason it was skipping. I have reverted my code from where I left off the other night when it worked and the app is still skipping the loginActivity. – James Boyle Jul 31 '18 at 20:26
  • Checkout this https://stackoverflow.com/a/35541827/3762067 – Varad Mondkar Jul 31 '18 at 20:27
  • @VaradMondkar so are you saying I should remove `user !=null` ? – James Boyle Jul 31 '18 at 20:29
  • Put a toast in the handler that shows the logged in user and some data. Maybe find out if you can ask that user object for some kind of status? Also for experimenting purposes, before attaching the handler, try to logout any users. – Squirrelkiller Jul 31 '18 at 20:30
  • 1
    Yes since your `MainActivity` is non auth activity, just call `FirebaseAuth.getInstance().signOut();` line on `MainActivity`. So your user will sign out when he comes to `MainActivity` – Varad Mondkar Jul 31 '18 at 20:31
  • Or put a check on `MainActivity` whether `user != null` and if its true (Which means user came to `MainActivity` even when he is signed in), intent him to `MapActivity` – Varad Mondkar Jul 31 '18 at 20:36
  • Thanks @VaradMondkar, should that be called in a method or can it be placed anywhere on MainActivity? – James Boyle Jul 31 '18 at 20:37
  • 1
    Call it within `onCreate` of `MainActivity` – Varad Mondkar Jul 31 '18 at 20:38
  • Thanks, that's fixed my issue. Thanks for your help :) – James Boyle Jul 31 '18 at 20:40
  • Happy Coding :) – Varad Mondkar Jul 31 '18 at 21:11

0 Answers0