3

目的的Android - 火力地堡 - 不同類型的用戶的登錄

允許不同類型的用戶登錄到它們各自的接口

說明用戶的

  • 類型(3) :

    1. 學生
    2. 教師
  • 每個用戶經由火力地堡驗證電子郵件註冊

  • 每個用戶應能夠訪問它們各自的接口,就好像不同的每個接口從其他

  • 每個用戶已經在Firebase數據庫中保存了各自的子節點。例如,在「用戶」下面有一個「學校」,其下是「學生」,「家長」和「老師」等用戶類型。

數據庫用於可視化目的

  • 用戶

- >學校

------>學生

------>父

------>教師

enter image description here

問題

由於我目前使用的火力地堡驗證電子郵件,我無法分辨用戶是在它們之間。

建議的解決方案是創建一個帶有3種類型用戶的單選按鈕,供用戶在登錄到應用程序時進行選擇。這意味着用戶必須輸入他們的電子郵件,密碼並選擇他們的用戶類型才能登錄。

問題是,如果「學生」用戶在單選按鈕上選擇「家長」或「教師」並使用學生電子郵件登錄,該應用仍會將「學生」電子郵件識別爲「家長」或 「老師」

LoginActivity類

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; 
         } 
        } 
       }); 
    } 
} 

我所提出的解決方案(不完全)

我在想,我應該^ h ardcode 「用戶代碼」 爲每個用戶

例如,每個用戶沿着它們各自的 「用戶代碼」

「學生」= 「Student123」

「父」= 「Parent123」

「教師「= 」Teacher123「

一個代碼示例將是這樣

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 
} 

UserCode將是一個EditText,它允許用戶輸入「userCode」,即使這個想法的概念類似於Radio Button的概念,它將提供更多的安全性,因爲「userCode」充當「輔助」用戶只能從我那裏知道密碼(管理員)。給我你的想法


>>>>>>>>>>>>>>>>>>>>>>>>>>> SOLUTION < < < < < < < < < < < < < < < < < < < < < < < < < < <

解決方案 - 說明

後用戶通過身份驗證通過火力地堡電子郵件驗證,一個If else語句將在「loginUser」功能,這將決定是否通過認證的用戶註冊爲內運行一個「學生」,「家長」或「老師」。

在註冊過程中,註冊該用戶的 「用戶類型」 變量。 「userType」將被存儲在數據庫樹中。這樣,你就可以把它叫做登錄會話

解決方案中,以驗證用戶的類型 - 示例代碼

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) { 

           } 
          }); 
         } 
        } 
       }); 
    } 
+0

你必須在firebase數據庫中添加1個'parameter'參數'type'。 –

+0

你的意思是 用戶 - >類型 - >學生 - >父 - >老師 – Student

+0

可以發佈你的'數據庫結構' –

回答

1

所有你需要的是一個方法來驗證用戶是家長,學生或老師在他們首次註冊該應用程序時。之後,您在Firebase數據庫的用戶信息部分中輸入「type」變量(type = Parent,student或teacher)。然後當他們登錄時,你可以檢索他們的'類型'。並顯示正確的相應界面。

+0

這意味着我將不得不使用'jRegisterGuardDatabase = FirebaseDatabase.getInstance()。getReference()。child(「Users」)。child(userSchool).child(「Type」)。child(「Student」)。 child(RegisterUserID);'如果「registeredEmail」等於上面的代碼,用戶將被提示到相應的接口? – Student

1
  • 你需要採取1更多parameter as a type

  • 其中,如果user's student login它很容易differentiatabledatabase

  • 您需要創建database table根據您的需要

很高興他lp you

+0

是的,你是對的!我編輯了我的問題,幷包含了我的登錄功能代碼。謝謝= D – Student

+0

最受歡迎的兄弟 –

+0

嘿威沙爾,你能幫我回答我有的其他問題嗎? https://stackoverflow.com/questions/46435225/android-firebase-send-users-to-chat-room – Student