User login in Firebase

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 06:11:59

问题


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.

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.

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.

I try this.

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

回答1:


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

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

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

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


来源:https://stackoverflow.com/questions/53244038/user-login-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!