0

I want to open a Admin activity when the defined admin e mail signs in, without using role based auth. I've tried the below but it keeps opening the profileActivity when I sign in with the admin@admin.com e mail, could anyone assist? Edited to include the full code. I'm trying to firstly check if the user trying to log in is registered and if they are, do they have an admin e mail address, if so, take them to the admin area, if not, take them to the profile area. Thanks

progressBar.setVisibility(View.VISIBLE);
    mAuth.signInWithEmailAndPassword(email, password). addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override

        public void onComplete(@NonNull Task<AuthResult> task) {
            String loggedInUserEmail = mAuth.getCurrentUser().getEmail();
            String adminEmailAddress = "admin@admin.com";
            progressBar.setVisibility(View.GONE);
            if(task.isSuccessful() && (loggedInUserEmail.equals(adminEmailAddress))) {
                finish(); // finish activity
                Intent intent = new Intent(MainActivity.this, adminActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);

            }



            if(task.isSuccessful()) {
                finish(); // finish activity
                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);


            }


            else
            {
                Toast.makeText(getApplicationContext(), task.getException().getMessage(),Toast.LENGTH_SHORT).show();
            }
        }
    });
JimC
  • 3
  • 4
  • Where is the duplicate in the question? I edited my code and it still doesn't load the correct activity . Thanks. – JimC Feb 05 '18 at 20:10
  • What is `mAuth.getCurrentUser()` returning? Also, you don't seem to checking the email of the current user by accessing the object of the currently authenticated user. You're just checking the value of a text field. – Doug Stevenson Feb 05 '18 at 20:33
  • Thanks for your reply, I just edited to include the full code. It just seems to be returning a standard user and not checking their e mail address is the e mail address that should be taken to the admin activity . Yh I understand about all I'm doing is checking a value, I wasn't sure how to check the e mail and looks like all I'm dong is comparing a text field rather than the actual e mail. – JimC Feb 05 '18 at 21:11

1 Answers1

0

The problem in your code is the following line:

if (mAuth.getCurrentUser() != null && email.equals("admin@admin.com"))

You are not verifying admin's email address at all. You are just verifying if mAuth.getCurrentUser() which is ok and second verifying if the text which is coming from the editTextEmail is equal to admin@admin.com, but is not enough. To solve this, you need to change the logic of your code. Please see the following code:

String loggedInUserEmail = mAuth.getCurrentUser().getEmail();
String adminEmailAddress = "admin@admin.com";

if(loggedInUserEmail.equals(adminEmailAddress)) {
    finish();
    startActivity(new Intent(this, adminActivity.class));
} else {
    finish();
    startActivity(new Intent(this, ProfileActivity.class));
}

Edit:

if(task.isSuccessful() && (loggedInUserEmail.equals(adminEmailAddress))) {
    finish(); // finish activity
    Intent intent = new Intent(MainActivity.this, adminActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
} else {
    finish(); // finish activity
    Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Ok thanks, that makes sense. Should I keep this within the onStart method or move to the userlogin method? as within here I'm already checking if the task is successful then send them to profile Activity. – JimC Feb 06 '18 at 12:01
  • if I kept it as that then regardless if the user has an admin e mail then they would still always be taken to the profile Activity rather than adminActivity – JimC Feb 06 '18 at 12:09
  • I should use this if statement within signInWithEmailAndPassword method because you need to get the current user from the `mAuth` object. – Alex Mamo Feb 06 '18 at 12:22
  • sure, I just edited my question with the updated code within the userLogin method, it doesn't work as when a normal user logs in the app crashes, the admin user is successfully logged in and transferred to the adminActivity. In order for normal users to log in too, should I have the code within the same IF statement as the admin code within the OnComplete method? Thanks – JimC Feb 06 '18 at 14:21
  • Yes, you need to use the `else statement` as i wrote you in my answer. Now it is crashing because when the normal user tries to login, isn't redirected to an activity at all. Does it work using the else statement? – Alex Mamo Feb 06 '18 at 14:25
  • I edited my question to show what I tried. Now log in works, admin is taken to the profile Activity rather than adminActivity but a normal user is successfully taken to profileactivity. Is it something to do with the intents? – JimC Feb 06 '18 at 14:40
  • cheers, well the admin isn't correctly taking me to the adminActivity that's the only issue, from the code I edited above it still takes me to the profileActivity – JimC Feb 06 '18 at 14:44
  • It means that you are not logged with the admin or the email address of the admin is not `admin@admin.com`. Can you verify this? – Alex Mamo Feb 06 '18 at 14:51
  • yh I'm logging in with the admin@admin.com e mail but still getting directed to the profileActivity when it should be going to the adminActivity. Have you seen my code edit in my question? does that look right to you? Thanks – JimC Feb 06 '18 at 14:58
  • No, it's wrong. See my updated answer. You need to use a full if else statement. Does it work now with this? – Alex Mamo Feb 06 '18 at 15:01
  • just seen your edited answer, I used a 2nd if because I had the exception at the bottom, I've just changed the profileActivity statement to else if and used an else for the exception checking – JimC Feb 06 '18 at 15:05
  • yes it works now thanks, how would I go about incorporating the exception with the toast into this too? – JimC Feb 06 '18 at 15:07
  • Marked as answer. Thanks for your help – JimC Feb 06 '18 at 15:14