Aim
Allow different types of Users to sign into their respective interfaces
Description
Types of Users (3):
- Student
- Parent
- Teacher
Each user is registered via Firebase Authentication Email
Each user should be able to access their respective interfaces as each interface if different from the other
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".
Database for visualizing purposes
- Users
-> School
------> Student
------> Parent
------> Teacher
Problem
Since I'm currently using Firebase Authentication Email, I am unable to distinguish between which user is which.
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.
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"
LoginActivity Class
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)&& !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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> 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;
}
}
});
}
}
My Proposed Solution (Incomplete)
I was thinking that I should hardcode "UserCode" for each user
For example, each user alongside their respective "Usercode"
"Student" = "Student123"
"Parent" = "Parent123"
"Teacher" = "Teacher123"
A Code Example will be like this
if(task.isSuccessful() && userCode.equals("Student123")){
//Send user to Student Interface
}else if(task.isSuccessful() && userCode.equals("Parent123")){
//Send user to Parent Interface
}else if(task.isSuccessful() && userCode.equals("Teacher123")){
//Send user to Teacher Interface
}
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
>>>>>>>>>>>>>>>>>>>>>>>>>>>SOLUTION<<<<<<<<<<<<<<<<<<<<<<<<<<<
Solution - Description
After the user is Authenticated 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".
- Note
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
Solution - Sample Code
private void loginUser(final String userLoginEmail, final String userLoginPassword) {
mAuthLogin.signInWithEmailAndPassword(userLoginEmail, userLoginPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> 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) {
}
});
}
}
});
}
All you need is a way to verify that the user is a Parent, Student, or Teacher when they first sign up for the app. After that you put a 'type' variable in their user info section of the Firebase Database (type = Parent, student or teacher). Then when they login, you retrieve their 'type'. And show the correct corresponding interface.