如何将用户重定向到Cloud Firestore中的特定活动? [英] How to redirect a user to a specific activity in Cloud Firestore?

查看:44
本文介绍了如何将用户重定向到Cloud Firestore中的特定活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase身份验证注册帐户时,我将电子邮件存储在2个类别的教师和学生中.我使用emailpassword将电子邮件添加到Firestore 2个不同类别的教师和学生.登录时,我想检查电子邮件属于哪个类别(教师或学生),该怎么办?我尝试了Firestore查询isequalsto,但没有区别.

While Registering an account using Firebase auth I stored emails in 2 categories Teacher and Student. I add emails to Firestore 2 different categories Teacher and Student with email and password. When I login I want to check that email belongs to which category (Teacher or Student) how can I do that I tried the firestore query isequalsto but it didn't differentiate.

这是我的数据库

老师

学生

还有其他可能的解决方法吗?

Any other possible solution?

这是我的登录活动代码

package com.example.dell.step;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
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.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

public class LoginActivity extends AppCompatActivity {


    private Button mRegbtn;
    private Button mloginbtn;
    private EditText mEmail;
    private EditText mPassword;

    private Toolbar mToolbar;

    private FirebaseAuth mAuth;

    private ProgressDialog mLoginDialog;
    private FirebaseFirestore firebaseFirestore;
    private CollectionReference collectionReference_I;
    private CollectionReference collectionReference_S;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        mAuth = FirebaseAuth.getInstance();

        //Progress Dialog

        mLoginDialog = new ProgressDialog(this);

        //FOr Toolbar

        mToolbar = findViewById(R.id.login_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Login Account");


        mloginbtn = findViewById(R.id.login_btn);
        mEmail = findViewById(R.id.login_email);
        mPassword = findViewById(R.id.login_password);

        mRegbtn = findViewById(R.id.login_reg_btn);

        firebaseFirestore = FirebaseFirestore.getInstance();

        mRegbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent regIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(regIntent);
                finish();

            }
        });


        mloginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = mEmail.getText().toString();
                String password = mPassword.getText().toString();

                if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
                    mLoginDialog.setTitle("Login User");
                    mLoginDialog.setMessage("Please wait while we login to your account :)");
                    //it stops cancel Dialog when user touch on screen
                    mLoginDialog.setCanceledOnTouchOutside(false);
                    mLoginDialog.show();

                    login_user(email, password);
                }
            }
        });


    }

    private void login_user(final String email, final String password) {

        collectionReference_I = firebaseFirestore.collection("Teacher");
        collectionReference_S = firebaseFirestore.collection("Student");

        collectionReference_I.whereEqualTo("teacher_email", email).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {

                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {


                        if (task.isSuccessful()) {

                            //Dismiss the progress Dialog
                            mLoginDialog.dismiss();


                            //user is registered
                            Toast.makeText(LoginActivity.this, "Welcome to Teacher Account", Toast.LENGTH_SHORT).show();
                            Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(mainIntent);
                            finish();


                        } else {

                            mLoginDialog.hide();

                            //if user is not registered
                            Toast.makeText(LoginActivity.this, "Cannot Log in Please check the form and try again!", Toast.LENGTH_LONG).show();

                        }

                    }
                });


            }
        });

        collectionReference_S.whereEqualTo("Student_email", email).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {

                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {


                        if (task.isSuccessful()) {

                            //Dismiss the progress Dialog
                            mLoginDialog.dismiss();


                            //user is registered
                            Toast.makeText(LoginActivity.this, "Welcome to Student Account", Toast.LENGTH_SHORT).show();
                            Intent mainIntent = new Intent(LoginActivity.this, StudentMain.class);
                            startActivity(mainIntent);
                            finish();


                        } else {

                            mLoginDialog.hide();

                            //if user is not registered
                            Toast.makeText(LoginActivity.this, "Cannot Log in Please check the form and try again!", Toast.LENGTH_LONG).show();

                        }

                    }
                });


            }
        });


    }
}

推荐答案

使用两个不同的集合,针对每种类型的用户一个都不是一个灵活的解决方案.另一个更简单的解决方案可能是:

Using two different collections, one for each type of your users isn't quite a flexible solutions. Another more simpler solution might be:

Firestore-root
   |
   --- users (collection)
        |
        --- uid (document)
        |    |
        |    --- email: "student@email.com"
        |    |
        |    --- password: "********"
        |    |
        |    --- type: "student"
        |
        --- uid (document)
             |
             --- email: "teacher@email.com"
             |
             --- password: "********"
             |
             --- type: "teacher"

看,我已经将用户的类型添加为用户对象的属性.现在,要根据电子邮件地址吸引用户,请使用以下代码行:

See, I have added the type of the user as a property of your user object. Now, to get a user based on the email address, please use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
Query query = usersRef.whereEqualTo("email", "student@email.com")
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                String email = document.getString("email");
                String password = document.getString("password");
                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(/* ... */);
            }
        }
    }
});

用户通过身份验证后,要将用户重定向到相应的活动,请使用以下代码行:

Once the user is authenticated, to redirect the user to the corresponding activity, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
usersRef.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String type = document.getString("type");
                if(type.equals("student")) {
                    startActivity(new Intent(MainActivity.this, Student.class));
                } else if (type.equals("teacher")) {
                    startActivity(new Intent(MainActivity.this, Teacher.class));
                }
            }
        }
    }
});

这篇关于如何将用户重定向到Cloud Firestore中的特定活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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