gpt4 book ai didi

java - Firebase 身份验证方法验证登录是否成功

转载 作者:行者123 更新时间:2023-12-01 19:57:50 24 4
gpt4 key购买 nike

我尝试创建一种方法,当我成功使用 Firebase 身份验证进行签名时,该方法可以为我提供 boolean 值,但问题是我总是得到 false,即使日志表明这不是真的。

这是我的方法:

public boolean signIn(String email, String password, final Context context) {
Log.d(TAG, "signIn:" + email);
final boolean[] resultado = {false};

mAuth = FirebaseAuth.getInstance();
// [START sign_in_with_email]
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
userFire = mAuth.getCurrentUser();

resultado[0] = true;

Log.w(TAG, "signInWithEmail:succes");



} else {
Log.w(TAG, "signInWithEmail:failure", task.getException());

}

}
});

return resultado[0];
}

知道如何确保我得到正确的结果吗?

最佳答案

Firebase API 是异步的,这意味着 signInWithEmailAndPassword 在调用后立即返回,并且将在一段时间后调用它返回的任务的回调。无法保证需要多长时间。由于该方法立即返回,因此您返回的值 (resultado[0]) 尚未从回调中填充。

基本上,您尝试从异步 API 同步返回值。这不是一个好主意。您应该按预期异步处理 API。

要详细了解 Firebase API 为什么是异步的以及您可以从它们中获得什么,read this article .

关于java - Firebase 身份验证方法验证登录是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com