用户在Firebase中的登录 [英] User login in Firebase

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

问题描述

我用Firebase构建了两个注册选项,一个可以注册为雇主,另一个选择为工人.我给了他们一个ID,每个雇主在ID的开头都收到了字母"e",而工人在ID的开头都收到了字母"w",以便我识别他们.

I built with Firebase two options for registration, one can register as an employer and another option to register as an worker. I gave an ID to them, and each employer received the letter 'e' at the beginning of the ID and the worker received the letter 'w' at the beginning of the ID so that I could identify them.

我还创造了与Firebase进行连接的可能性,我希望如果以工作人员身份进行连接,我将切换到某个屏幕,如果以雇主身份进行连接,则将切换到另一个屏幕.

I also created the possibility of connecting with Firebase and I want if I connect as a worker I will switch to a certain screen and if I connect as an employer I will switch to another screen.

我将信息存储在Firestore中.集合中的每个文档都会在文档中接收该用户的UID.用字母找到ID.

I store the information inside the Firestore. Each document in the collection receives the UID of that user within the document. The ID is found with the letters.

我尝试这个.

private char s = 'e';
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {

        if(task.isSuccessful())
        {

            mDatabase.collection("employer").document(mUser.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful())
                    {
                        DocumentSnapshot documentSnapshot = task.getResult();
                        index = documentSnapshot.getString("ID");
                        ID = index.charAt(0);
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    mDatabase.collection("worker").document(mUser.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                            DocumentSnapshot documentSnapshot = task.getResult();
                            index = documentSnapshot.getString("ID");
                            ID = index.charAt(0);
                        }
                    });
                }
            });

            if(mUser.isEmailVerified())
            {

                if(ID == s)
                {
                    Intent k = new Intent(login_page.this,home_screen_employer.class);
                    startActivity(k);
                }
                else {
                    Intent k = new Intent(login_page.this,home_screen_worker.class);
                    startActivity(k);
                }
        }

推荐答案

数据是从Firestore异步加载的.到您的 if(ID == s)运行时,数据尚未加载.

Data is loaded from Firestore asynchronously. By the time your if(ID == s) runs, the data hasn't been loaded yet.

您需要在 onComplete 方法的内部中调用重定向(构建意图并启动活动)的代码.

You'll need call the code that redirects (builds the intent and starts the activity) inside the onComplete methods.

因此,首先定义一个函数:

So first define a function:

public void redirect(User user, char id) {
    if(user.isEmailVerified()) {
        if (id == 'e') {
            Intent k = new Intent(login_page.this,home_screen_employer.class);
            startActivity(k);
        }
        else {
            Intent k = new Intent(login_page.this,home_screen_worker.class);
            startActivity(k);
        }
    }
}

然后从您的两个 onComplete 方法调用它:

And then call it from your two onComplete methods:

mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  @Override
  public void onComplete(@NonNull Task<AuthResult> task) {
    if(task.isSuccessful()) {
      mDatabase.collection("employer").document(mUser.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful())
                {
                    DocumentSnapshot documentSnapshot = task.getResult();
                    index = documentSnapshot.getString("ID");
                    redirect(mUser, index.charAt(0));
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                mDatabase.collection("worker").document(mUser.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        DocumentSnapshot documentSnapshot = task.getResult();
                        index = documentSnapshot.getString("ID");
                        redirect(mUser, index.charAt(0));
                    }
                });
            }
        });


稍微重构一下以使用成功的侦听器,会导致:


Refactoring this a bit to use success listeners, leads to:

mAuth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
  @Override
  public void onSuccess(AuthResult authResult) {
  mDatabase.collection("employer").document(mUser.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot doc) {
            index = doc.getString("ID");
            redirect(mUser, index.charAt(0));
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            mDatabase.collection("worker").document(mUser.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot doc) {
                    index = doc.getString("ID");
                    redirect(mUser, index.charAt(0));
                }
            });
        }
    });

然后进行重构以消除重复的文档分析:

And then refactoring to get rid of the duplicated document-parsing:

mAuth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
  @Override
  public void onSuccess(AuthResult authResult) {
  mDatabase.collection("employer").document(mUser.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot doc) {
            redirect(mUser, doc);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            mDatabase.collection("worker").document(mUser.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot doc) {
                    redirect(mUser, doc);
                }
            });
        }
    });

使用 redirect 函数的更新后的定义:

With this updated definition of the redirect function:

public void redirect(User user, Document doc) {
    if(user.isEmailVerified()) {
        char id = doc.getString("ID").charAt(0);
        if (id == 'e') {
            Intent k = new Intent(login_page.this,home_screen_employer.class);
            startActivity(k);
        }
        else {
            Intent k = new Intent(login_page.this,home_screen_worker.class);
            startActivity(k);
        }
    }
}

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

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