title: Android - Firebase - Different types of Users Login
body: <p><strong>Aim</strong></p>
  
  <p>Allow different types of Users to sign into their respective interfaces</p>
  
  <p><strong>Description</strong></p>
  
  <ul>
  <li><p>Types of Users (3): </p>
  
  <ol>
  <li>Student</li>
  <li>Parent</li>
  <li>Teacher</li>
  </ol></li>
  <li><p>Each user is registered via Firebase Authentication Email</p></li>
  <li><p>Each user should be able to access their respective interfaces as each interface if different from the other</p></li>
  <li><p>Each user has already been saved with their respective child nodes within the Firebase Database. For example, under the "Users" there's a "School" and under that are the user types such as "Student", "Parent", and "Teacher".</p></li>
  </ul>
  
  <p>Database for visualizing purposes</p>
  
  <ul>
  <li>Users</li>
  </ul>
  
  <p>-> School</p>
  
  <p>------> Student</p>
  
  <p>------> Parent</p>
  
  <p>------> Teacher</p>
  
  <p><a href="https://i.stack.imgur.com/L2Ist.png" rel="noreferrer"><img src="https://i.stack.imgur.com/L2Ist.png" alt="enter image description here"></a></p>
  
  <p><strong>Problem</strong></p>
  
  <p>Since I'm currently using Firebase Authentication Email, I am unable to distinguish between which user is which. </p>
  
  <p>A proposed solution which is to create a Radio Button with 3 type of users for the user to select when logging into the app. This means that the user will have to sign in by entering their email, password, and selecting their type of user. </p>
  
  <p>The Problem with this is that what if the "Student" user selects "Parent" or "Teacher" on the Radio Button and sign in using a Student email, the app will still recognize the "Student" email as a "Parent" or "Teacher"</p>
  
  <p><strong>LoginActivity Class</strong></p>
  
  <pre><code>import android.content.Intent;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.Toolbar;
  import android.text.TextUtils;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.AuthResult;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseAuthException;
  
  public class LoginActivity extends AppCompatActivity {
  
      private Toolbar jLoginToolbar;
  
      private EditText jLoginEmail;
      private EditText jLoginPassword;
  
      private Button jLoginBtn;
      private Button jAdminLoginBtn;
      private FirebaseAuth mAuth;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
  
          mAuth = FirebaseAuth.getInstance();
  
          jLoginToolbar = (Toolbar) findViewById(R.id.loginToolbar);
          setSupportActionBar(jLoginToolbar);
          getSupportActionBar().setTitle("Account Login");
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
          jLoginEmail = (EditText) findViewById(R.id.loginEmail);
          jLoginPassword = (EditText) findViewById(R.id.loginPassword);
          jLoginBtn = (Button) findViewById(R.id.loginBtn);
          jAdminLoginBtn = (Button) findViewById(R.id.loginAdminBtn);
  
          jAdminLoginBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                      Intent intentAdmin = new Intent(LoginActivity.this, AdminLoginActivity.class);
                      startActivity(intentAdmin);
              }
          });
  
          jLoginBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String userLoginEmail = jLoginEmail.getText().toString();
                  String userLoginPassword = jLoginPassword.getText().toString();
  
                  if(!TextUtils.isEmpty(userLoginEmail)&amp;&amp; !TextUtils.isEmpty(userLoginPassword)) {
                      loginUser(userLoginEmail, userLoginPassword);
                  }else{
                      Toast.makeText(LoginActivity.this, "Failed Login: Empty Inputs are not allowed", Toast.LENGTH_SHORT).show();
                  }
              }
          });
  
      }
  
      private void loginUser(final String userLoginEmail, final String userLoginPassword) {
          mAuth.signInWithEmailAndPassword(userLoginEmail, userLoginPassword)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if (task.isSuccessful()) {
                              Intent intentMain = new Intent(LoginActivity.this, MainActivity.class);
                              intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                              startActivity(intentMain);
                              finish();
                          }else{
                              FirebaseAuthException e = (FirebaseAuthException )task.getException();
                              Toast.makeText(LoginActivity.this, "Failed Login: "+e.getMessage(), Toast.LENGTH_SHORT).show();
                              return;
                          }
                      }
                  });
      }
  }
  </code></pre>
  
  <p><strong>My Proposed Solution (Incomplete)</strong></p>
  
  <p>I was thinking that I should hardcode "UserCode" for each user</p>
  
  <p>For example, each user alongside their respective "Usercode"</p>
  
  <p>"Student" = "Student123"</p>
  
  <p>"Parent" = "Parent123"</p>
  
  <p>"Teacher" = "Teacher123"</p>
  
  <p>A Code Example will be like this</p>
  
  <pre><code>    if(task.isSuccessful() &amp;&amp; userCode.equals("Student123")){
      //Send user to Student Interface
  }else if(task.isSuccessful() &amp;&amp; userCode.equals("Parent123")){
      //Send user to Parent Interface
  }else if(task.isSuccessful() &amp;&amp; userCode.equals("Teacher123")){
      //Send user to Teacher Interface
  }
  </code></pre>
  
  <p>The UserCode will be an EditText which will allow the user to enter the "userCode" even though the concept of this idea is similar to that of the Radio Button, it will provide more security as the "userCode" acts as a "Secondary" password in which the Users will only know it from me (Admin). Give me your thoughts</p>
  
  <hr>
  
  <p><strong>>>>>>>>>>>>>>>>>>>>>>>>>>>>SOLUTION&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</strong></p>
  
  <p><strong>Solution - Description</strong></p>
  
  <p>After the user is <strong>Authenticated</strong> via Firebase Email Authentication, an If else statement will be run within the "loginUser" function which will determine if the Authenticated user was registered as a "Student", "Parent" or "Teacher". </p>
  
  <ul>
  <li>Note</li>
  </ul>
  
  <p>During registration, register a "userType" variable for the user. The "userType" will be stored within the Database Tree. That way you will be able to call it to verify the type of user during the login session</p>
  
  <p><strong>Solution - Sample Code</strong></p>
  
  <pre><code>private void loginUser(final String userLoginEmail, final String userLoginPassword) {
          mAuthLogin.signInWithEmailAndPassword(userLoginEmail, userLoginPassword)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if(task.isSuccessful()){
  
                              FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
                              String RegisteredUserID = currentUser.getUid();
  
                              jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(RegisteredUserID);
  
                              jLoginDatabase.addValueEventListener(new ValueEventListener() {
                                  @Override
                                  public void onDataChange(DataSnapshot dataSnapshot) {
                                      String userType = dataSnapshot.child("userType").getValue().toString();
                                      if(userType.equals("Resident")){
                                          Intent intentResident = new Intent(LoginActivity.this, ResidentActivity.class);
                                          intentResident.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                          startActivity(intentResident);
                                          finish();
                                      }else if(userType.equals("Guard")){
                                          Intent intentMain = new Intent(LoginActivity.this, SecurityGuardActivity.class);
                                          intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                          startActivity(intentMain);
                                          finish();
                                      }else if(userType.equals("Police")){
                                          Intent intentMain = new Intent(LoginActivity.this, PoliceActivity.class);
                                          intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                          startActivity(intentMain);
                                          finish();
                                      }else{
                                          Toast.makeText(LoginActivity.this, "Failed Login. Please Try Again", Toast.LENGTH_SHORT).show();
                                          return;
                                      }
                                  }
  
                                  @Override
                                  public void onCancelled(DatabaseError databaseError) {
  
                                  }
                              });
                          }
                      }
                  });
      }
  </code></pre>