0

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);
                }
        }
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
matan
  • 97
  • 13

1 Answers1

2

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);
        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I try this but in the redirect function the user and the doc unrecognized. Also how i do if the email not verified it wiil transfer to other activity ? – matan Nov 11 '18 at 07:46
  • I tried this code but the appliction crash when i try to connect as a worker – matan Nov 11 '18 at 14:25
  • I took `mUser` and the document from your code and pass them into the new `redirect` function. There may be typos in my code, but those should be minor. If it's not something you can figure out, update your question with what you've tried. – Frank van Puffelen Nov 11 '18 at 15:10
  • Its recognize the redirect function but when i try to connect as worker the app crash – matan Nov 11 '18 at 18:01