Firebase Auth返回错误的值

时间:2018-05-12 22:01:05

标签: android firebase firebase-authentication

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;

                        }
                    }
                }
            });

    }
}

这段代码在用户登录时返回枚举值。当应用程序启动时,状态应为SIGNED_OUT,并且当FirebaseAuth登录成功时,它应更改为SIGNIN_SUCCESS。但是它总是返回SIGNED_OUT。

更多详情:

  • 本地状态对象的值在onComplete和onFail函数内更改
  • 验证完成时返回此值

提前致谢

1 个答案:

答案 0 :(得分:1)

现在尚未归还尚未加载的内容。换句话说,您不能简单地使用userAuthStatus方法之外的onComplete()对象,因为由于此方法的异步行为,它始终具有默认值Status.SIGNED_OUT。这意味着当您尝试在该方法Log.w(TAG, "returns" + userAuthStatus);之外使用该结果时,数据尚未从数据库中完成加载,这就是为什么无法访问的原因。快速解决这个问题的方法是仅在onComplete()方法中使用该结果,或者如果你想在该方法之外使用它,我建议你从这个 {{看到我的anwser的最后一部分3}} 我已经开始使用自定义回调来完成它的完成。您还可以查看此 post ,以便更好地理解。