如果调用了firebaseAuth.signOut(),则无法发送用户数据

时间:2019-05-30 06:29:04

标签: android firebase firebase-realtime-database firebase-authentication

我对为什么会这样感到困惑。

我想在注册活动中更新我的用户个人资料,一切正常,并且数据库已更新,直到我将firebaseAuth.signOut()添加到sendUserData()函数。有人遇到过这个问题吗?

package com.example.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterActivity extends AppCompatActivity {

    private EditText userName, userPassword, userEmail, phoneNumber;
    private Button logButton, regButton;
    private FirebaseAuth firebaseAuth;
    String email, name, phone, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setupUIViews();

        firebaseAuth = FirebaseAuth.getInstance();

        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(validate()){
                    //upload everithing to the database
                    String user_name = userName.getText().toString().trim();
                    String user_email = userEmail.getText().toString().trim();
                    String user_password = userPassword.getText().toString().trim();
                    String user_phone = phoneNumber.getText().toString().trim();

                    firebaseAuth.createUserWithEmailAndPassword(user_email, user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful()) {
                                sendEmailVerification();
                            }
                            else{
                                Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            }
        });
        logButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
            }
        });
    }

    private void setupUIViews(){
        userName = (EditText)findViewById(R.id.etUser2);
        userPassword = (EditText)findViewById(R.id.etPas2);
        userEmail = (EditText)findViewById(R.id.etEmail2);
        phoneNumber = (EditText)findViewById(R.id.etPhone1);
        regButton = (Button)findViewById(R.id.registerButton2);
        logButton = (Button)findViewById(R.id.loginButton2);

    }

    private Boolean validate(){
        Boolean result = false;

         name = userName.getText().toString();
         email = userEmail.getText().toString();
         phone = phoneNumber.getText().toString();
         password = userPassword.getText().toString();

        if(name.isEmpty() || password.isEmpty() || email.isEmpty()){
           Toast.makeText(this, "Please enter all the details", Toast.LENGTH_SHORT).show();
        }
        else{
            result = true;
        }
        return result;
    }

    private void sendEmailVerification(){
        final FirebaseUser firebaseUser = firebaseAuth.getInstance().getCurrentUser();

        if(firebaseUser != null){
            firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        sendUserData();
                        Toast.makeText(RegisterActivity.this, "Sucessfully registered", Toast.LENGTH_SHORT).show();
 //---------------->>>//firebaseAuth.signOut();//<<<-----------//
                        finish();
                        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                    }
                    else{
                        Toast.makeText(RegisterActivity.this, "Verification email was not sent", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void sendUserData(){
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
        UserProfile userProfile = new UserProfile(name, email, phone);

        Toast.makeText(RegisterActivity.this, "User data was sent to the database" + firebaseDatabase.toString(), Toast.LENGTH_SHORT).show();

        myRef.setValue(userProfile).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                firebaseAuth.signOut();
            }
        });
    }
}

因此数据库可以正常运行(数据通过firebase被上载到实时数据库中)而无需注销,但是我遇到的问题是用户自动登录到UserProfileActivity中。

1 个答案:

答案 0 :(得分:0)

请提供错误跟踪信息或错误信息,以便我们了解发生了什么。顺便说一句,您的吐司应该显示在onSuccessListener()内。在检查任务是否成功之前,您正在显示吐司