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

查看:287
本文介绍了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


    • 用户

    - >学校

    ------>学生

    ------>父母

    ------>老师

    < a href =https://i.stack.imgur.com/L2Ist.png =nofollow noreferrer>

    问题

    自我目前正在使用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 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;
                            }
                        }
                    });
        }
    }
    

    我建议的解决方案(不完整)

    我以为我应该为每个用户硬编码UserCode

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

    例如,每个用户与他们各自的用户代码一起

    For example, each user alongside their respective "Usercode"

    学生=学生123

    父母=父母123

    老师=老师123

    代码示例将是这样的

        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,即使这个想法的概念类似于t的概念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

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

    解决方案 - 说明

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

    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数据库的用户信息部分(type = Parent,student或teacher)中添加了type变量。然后,当他们登录时,您将检索他们的类型。并显示正确的相应界面。

    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天全站免登陆