Android - Firebase - 不同类型的用户登录 [英] Android - Firebase - Different types of Users Login

查看:18
本文介绍了Android - Firebase - 不同类型的用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

允许不同类型的用户登录各自的界面

Allow different types of Users to sign into their respective interfaces

说明

  • 用户类型 (3):

  • Types of Users (3):

  1. 学生
  2. 家长
  3. 老师

  • 每个用户都通过 Firebase 身份验证电子邮件进行注册

  • 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

    每个用户已经与他们各自的子节点一起保存在 Firebase 数据库中.例如,在用户"下有一个学校",在其下是学生"、家长"和教师"等用户类型.

    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

    • 用户

    -> 学校

    ------> 学生

    ------> 父母

    ------> 老师

    问题

    由于我目前正在使用 Firebase 身份验证电子邮件,我无法区分哪个用户是哪个.

    Since I'm currently using Firebase Authentication Email, I am unable to distinguish between which user is which.

    建议的解决方案是创建一个具有 3 类用户的单选按钮,供用户在登录应用程序时进行选择.这意味着用户必须通过输入他们的电子邮件、密码并选择他们的用户类型来登录.

    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 类

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

    我提出的解决方案(不完整)

    我想我应该为每个用户硬编码用户代码"

    I was thinking that I should hardcode "UserCode" for each user

    例如,每个用户及其各自的用户代码"

    For example, each user alongside their respective "Usercode"

    "Student" = "Student123"

    "Student" = "Student123"

    "Parent" = "Parent123"

    "Parent" = "Parent123"

    "Teacher" = "Teacher123"

    "Teacher" = "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"充当二级"密码,用户只能从我(管理员)那里知道它.给我你的想法

    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

    >>>>>>>>>>>>>>>>>>>>>>>>>解决方案;<<<<<<<<<<<<<<<<<<

    解决方案 - 描述

    在用户通过 Firebase 电子邮件身份验证进行身份验证后,将在loginUser"函​​数中运行 If else 语句,该语句将确定经过身份验证的用户是否已注册为学生"、家长""或老师".

    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".

    • 注意

    在注册过程中,为用户注册一个userType"变量.userType"将存储在数据库树中.这样你就可以在登录会话期间调用它来验证用户类型

    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

    解决方案 - 示例代码

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

    推荐答案

    您所需要的只是在用户首次注册应用程序时验证其是家长、学生或教师的方法.之后,您将类型"变量放入 Firebase 数据库的用户信息部分(类型 = 家长、学生或教师).然后,当他们登录时,您会检索他们的类型".并显示正确对应的界面.

    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.

    这篇关于Android - Firebase - 不同类型的用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆