我想在定义的管理员电子邮件登录时打开管理员活动,而不使用基于角色的身份验证。我尝试了以下但是当我使用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();
}
}
});
答案 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);
}