if语句检查数据库信息时,如果不应该这样做

时间:2018-04-06 18:52:51

标签: java android firebase firebase-authentication

我试图检查登录活动时出错,如果检测到以下问题,它会触发意图:

1)如果用户没有注册(他使用的电子邮件未经过firebase认证),这对我来说效果很好

2)如果用户已注册但未向firebase数据库提供任何信息

我的问题是,出于某种原因,我用来检查数据库中的信息的代码,即使他们的数据库中的信息附加到他们的UID,也适用于用户。

意味着告诉他们提供信息的意图将在他们已经提供信息时触发。

if(task.isSuccessful()){
    // Checks if user has submitted information in the Essential Information activity
    //Takes the Unique ID(if it is present if not it will tell him to sign up or invalid email) asks the firebase database if he has given information to the database
    reference.child("Users").child(UserUID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // User exists
            if (dataSnapshot.exists()) {
                //Displays Toast telling user that their information is saved in the database
                Toast.makeText(LogInActivity.this, "You have data in our database ", Toast.LENGTH_SHORT).show();
            }
            //User doesn't have information in the database
            else {
                // Displays Toast telling user he/she needs to sign in into the firebase database
                // User goes to UserInformationActivity to give his/her information
                Toast.makeText(LogInActivity.this, "You need to give Essential Information", Toast.LENGTH_SHORT).show();

                // 3 second delay
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Goes to UserInformationActivity
                        Intent GoToUserInformation = new Intent(LogInActivity.this, UserInformationActivity.class);
                        LogInActivity.this.startActivity(GoToUserInformation);
                    }
                }, 3000);

            }


        }
        // if the checking got cancelled, likability of that happening is small
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
}

1 个答案:

答案 0 :(得分:0)

您不想检查用户是否已登录?

直接来自火场...

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
    // check a snapshot to see if there is information....if not, error out.
} else {
    // No user is signed in
}