如何使用Firebase数据库创建单独的登录帐户?

时间:2019-05-19 20:30:07

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

Firebase

我想问一下如何创建两个单独的登录意图。例如,一个用于客户,一个用于管理员检查详细信息。

private void userLogin()
{
    String email = emailText.getText().toString().trim();
    String password = passText.getText().toString().trim();

    if(email.isEmpty())
    {
        emailText.setError("Email is required");
        passText.requestFocus();
        return;
    }
    if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
    {
        emailText.setError("Please Enter a Valid email");
        passText.requestFocus();
        return;
    }
    if(password.isEmpty())
    {
        emailText.setError("Password is required");
        passText.requestFocus();
        return;
    }
    if(password.length()<6)
    {
        emailText.setError("Minimum length of Password is 6");
        passText.requestFocus();
    }
    mProgress.show();
    mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful())
            {
                Intent i = new Intent(MainActivity.this, Placeorder.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
                        Intent.FLAG_ACTIVITY_NEW_TASK); //Clears all activity and open a new one
                startActivity(i);
            }
            else
            {
                mProgress.dismiss();
                Toast.makeText(getApplicationContext(),task.getException().getMessage(),Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void onLoginClick(View view) {

    userLogin();
}

在此代码中,它只能显示客户部分,但是如何在管理员或客户和管理员分别使用的地方输入另一种方式?

已更新

mProgress.show();
    mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful())
            {
                FirebaseUser user = mAuth.getCurrentUser();
                String userID = user.getUid();
                myRef = FirebaseDatabase.getInstance().getReference(userID);
                myRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        String value = dataSnapshot.getValue(String.class);
                        if(value == "Grocer")
                        {
                            Intent i = new Intent(MainActivity.this, Placeorder.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                                    Intent.FLAG_ACTIVITY_CLEAR_TASK |
                                    Intent.FLAG_ACTIVITY_NEW_TASK); //Clears all activity and open a new one
                            startActivity(i);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

Firebase Database

Firebase updated sturcture database

代码以构成结构

myRef = FirebaseDatabase.getInstance().getReference(userID).child("Grocer");    
mAuth.createUserWithEmailAndPassword(email1,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()) {
                        //progressBar.setVisibility(View.GONE);
                        FirebaseUser user = mAuth.getCurrentUser();
                        String userID = user.getUid();
                        myRef.child("First Name").setValue(first_name1);
                        myRef.child("Last Name").setValue(last_name1);
                        myRef.child("Email").setValue(email1);
                        myRef.child("Number").setValue(numbers1);
                        myRef.child("Grocer").setValue(grocer);
                        myRef.child("isAdmin").setValue(true);

1 个答案:

答案 0 :(得分:0)

您必须在数据库中设置一个检查项,然后当用户登录应用程序时,您应该在数据库中检查用户是客户还是管理员之后,您必须根据应用程序来传递意图,在其中设置一个布尔值像这样的数据库:-

boolean isAdmin ; 

并且当用户登录您的应用程序时,比较uid将被注册到admin或customer中,并在isAdmin提交的结果中将结果设置为true或false:-

if(isAdmin){
// user is admin
}else{
// user is customer
}
相关问题