Sachin Titus
Sachin Titus

Reputation: 2339

Firebase Auth returns wrong value

import android.support.annotation.NonNull;
import android.util.Log;

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.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthInvalidUserException;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.FirebaseUser;
import com.sachintitus.instafy.instafy.repository.Model.Status;


public class FirebaseAuthentication implements Authentication {

    public static String TAG = "FIREBASEAUTHENTICATION";

    public Status userAuthStatus;
    private FirebaseAuth mAuth;

    public FirebaseAuthentication() {
        mAuth = FirebaseAuth.getInstance();
        userAuthStatus = Status.SIGNED_OUT;
    }

    @Override
    public Status signIn(String email, String password) {
        Log.w("AUTH", "Started");
        mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = mAuth.getCurrentUser();

                        userAuthStatus = Status.SIGNIN_SUCCESS;
                        Log.w(TAG, "signInWithEmail:success");
                        Log.w(TAG, String.valueOf(userAuthStatus));

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithEmail:failure", task.getException());

                        if (task.getException() instanceof FirebaseAuthInvalidUserException) {
                            Log.w(TAG, "signInWithEmail: failure", task.getException());
                            userAuthStatus = Status.SIGNIN_FAILED_NEW_USER;
                        }
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            Log.w(TAG, "SignInWithEmail: Faulure", task.getException());
                            userAuthStatus = Status.SIGNIN_FAILED_WRONG_PASSWORD;
                        }

                    }

                    // ...
                }
            });
        Log.w(TAG, "returns" + userAuthStatus);
        return userAuthStatus;
    }

    @Override
    public void signUp(String email, String password) {

        mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        userAuthStatus = Status.SIGNUP_SUCCESS;

                    } else {
                        // If sign up fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());

                        if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                            Log.w(TAG, "User already exists, sign up failed", task.getException());
                            userAuthStatus = Status.SIGNUP_FAILED_EXISTING_USER;

                        }
                    }
                }
            });

    }
}

This piece of code returns Enum values as the user is signed in. When the app started, the status should be SIGNED_OUT, and it should change to SIGNIN_SUCCESS as FirebaseAuth sign in is successful. However it always returns SIGNED_OUT.

More details:

Thanks in advance

Upvotes: 0

Views: 395

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You cannot return something now that hasn't been loaded yet. With other words, you cannot simply use the userAuthStatus object outside the onComplete() method because it will always have the default value of Status.SIGNED_OUT due the asynchronous behaviour of this method. This means that by the time you are trying to use that result outside that method Log.w(TAG, "returns" + userAuthStatus);, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use that result only inside the onComplete() method, or if you want to use it outside that method, I recommend you see the last part of my anwser from this post in which I have exaplined how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Upvotes: 1

Related Questions