SignOut无法保存在firestore中

时间:2018-05-29 15:43:26

标签: android firebase-authentication google-cloud-firestore

我正在为一个应用程序进行注册活动,有些奇怪的事情可以帮助我吗?

这是注册的代码,如果我对signOut函数发表评论,但是在FirebaseAuth和Firestore中保存数据后我需要注销它。

我连日志都没有。

public void registUser() {

    mAuth = FirebaseAuth.getInstance();
    mAuth.createUserWithEmailAndPassword(user.getEmail(), user.getPassword()).addOnCompleteListener(SignUpUserActivity.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {

                Toast.makeText(SignUpUserActivity.this, "Sucesso", Toast.LENGTH_SHORT).show();
                // Sign in success, update UI with the signed-in user's information
                //FirebaseUser user = mAuth.getCurrentUser();
                user.setId(task.getResult().getUser().getUid());

                user.save();

                mAuth.signOut();
                finish();



            } else {
                String erro = "";
                try {
                    throw task.getException();


                } catch (FirebaseAuthWeakPasswordException e) {
                    erro = "Digite uma senha mais forte";
                } catch (FirebaseAuthInvalidCredentialsException e) {
                    erro = "Email invalido";
                } catch (FirebaseAuthUserCollisionException e) {
                    erro = "Email Registado";
                } catch (Exception e) {
                    erro = "Erro generico";
                    e.printStackTrace();
                }
                Toast.makeText(SignUpUserActivity.this, erro, Toast.LENGTH_SHORT).show();

            }

        }

    });
}

我认为它是因为在数据库中保存东西需要很长时间。 我对Auth部件没有任何问题。

谢谢。

这是保存功能。

 public void save(){


    DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put("Name", name);
    dataToSave.put("email", email);
    dataToSave.put("clinic", clinic);
    dataToSave.put("Job", job);
    reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d("artur", "Saved");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w("artur", "Not Saved",e);
        }
    });
}

2 个答案:

答案 0 :(得分:2)

要解决此问题,请使用以下代码:

user.setId(task.getResult().getUser().getUid());
userRef.set(user).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        mAuth.signOut();
    }
});

退出时,您需要确保您的写入操作已完成,而这只能通过addOnSuccessListener实现。

答案 1 :(得分:0)

public void save(){


    DocumentReference reference = FirebaseFirestore.getInstance().document("/Clinicas/" + clinic + "/Profissionais/" + id + "/");
    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put("Name", name);
    dataToSave.put("email", email);
    dataToSave.put("clinic", clinic);
    dataToSave.put("Job", job);
    reference.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {

        @Override
        public void onSuccess(Void aVoid) {
            Log.d("artur", "Saved");
            mAuth = FirebaseAuth.getInstance();
            mAuth.signOut();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w("artur", "Not Saved",e);
        }
    });




}