如何在Android App中使用Firebase Auth为某些电子邮件地址用户打开不同的活动

时间:2018-02-05 19:57:22

标签: java android firebase firebase-authentication

我想在定义的管理员电子邮件登录时打开管理员活动,而不使用基于角色的身份验证。我尝试了以下但是当我使用admin@admin.com电子邮件登录时,它会一直打开profileActivity,有人可以帮忙吗?编辑包含完整代码。我试图首先检查用户是否已注册,如果是,他们是否有管理员电子邮件地址,如果是,请将他们带到管理区域,如果没有,请将他们带到配置文件区域。感谢

progressBar.setVisibility(View.VISIBLE);
    mAuth.signInWithEmailAndPassword(email, password). addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override

        public void onComplete(@NonNull Task<AuthResult> task) {
            String loggedInUserEmail = mAuth.getCurrentUser().getEmail();
            String adminEmailAddress = "admin@admin.com";
            progressBar.setVisibility(View.GONE);
            if(task.isSuccessful() && (loggedInUserEmail.equals(adminEmailAddress))) {
                finish(); // finish activity
                Intent intent = new Intent(MainActivity.this, adminActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);

            }



            if(task.isSuccessful()) {
                finish(); // finish activity
                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);


            }


            else
            {
                Toast.makeText(getApplicationContext(), task.getException().getMessage(),Toast.LENGTH_SHORT).show();
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

代码中的问题如下:

if (mAuth.getCurrentUser() != null && email.equals("admin@admin.com"))

您根本没有验证管理员的电子邮件地址。您只是验证mAuth.getCurrentUser()是否正常,然后再验证来自editTextEmail的文本是否等于admin@admin.com,但是还不够。要解决此问题,您需要更改代码的逻辑。请参阅以下代码:

String loggedInUserEmail = mAuth.getCurrentUser().getEmail();
String adminEmailAddress = "admin@admin.com";

if(loggedInUserEmail.equals(adminEmailAddress)) {
    finish();
    startActivity(new Intent(this, adminActivity.class));
} else {
    finish();
    startActivity(new Intent(this, ProfileActivity.class));
}

修改

if(task.isSuccessful() && (loggedInUserEmail.equals(adminEmailAddress))) {
    finish(); // finish activity
    Intent intent = new Intent(MainActivity.this, adminActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
} else {
    finish(); // finish activity
    Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}