Android:如果函数运行时没有用户唯一ID,并且用户未经过Firebase身份验证

时间:2018-04-05 15:08:17

标签: java android firebase firebase-authentication

我尝试检查连接到Firebase的登录屏幕中的错误,我使用的OnCompleteListener检查是否:

1)用户已经注册

2)如果用户在我的数据库中提供了信息

每当用户输入未经过身份验证的电子邮件时,它也会告诉他提供信息(检查是否存在空的唯一ID),但它不应该发生,或者换句话说,为什么if(task.isComplete() && !task.isSuccessful()) 没有唯一ID且没有用户

时运行

这里是OnCompleteListener代码,忽略OnSuccesslistener一个

Task<AuthResult> authResultTask = firebaseAuth.signInWithEmailAndPassword(Email, Password)
    // if login is complete
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            // dismisses progressbar and checks if the users has already given Essential Information, 
            // If not it will take him to the essential information activity
            progressDialog.dismiss();

            if(task.isComplete() && !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
                FirebaseUser user = firebaseAuth.getCurrentUser();
                String UserUID = user.getUid();
                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) {

                    }

                });
            }

            if (!task.isSuccessful()) {
                try {
                    throw task.getException();
                } catch(FirebaseAuthInvalidUserException e) {
                    Toast.makeText(LogInActivity.this, "Invalid email, You need to sign up first", Toast.LENGTH_SHORT).show();
                    // 3 second delay
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // Goes to UserInformationActivity
                            Intent Signup = new Intent(LogInActivity.this, SignupActivity.class);
                            LogInActivity.this.startActivity(Signup);
                        }
                    }, 3000);
                } catch(Exception e) {
                    Toast.makeText(LogInActivity.this, "", Toast.LENGTH_SHORT).show();
                    Log.e("Exception", e.getMessage());
                }
            }

        }

    })
    .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {
            Toast.makeText(LogInActivity.this, "SuccesListener working", Toast.LENGTH_SHORT).show();
        }
    });

0 个答案:

没有答案