如何在android中的firebase中分隔两个用户

时间:2017-08-21 13:34:08

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

在我的应用程序中,我有两个用户 - 活动会员和客户 - 他们有单独的用户登录和注册。如果客户登录,他将转到客户端活动;如果活动成员将登录,他将转到活动成员活动。我如何确保电子邮件是客户或活动成员?

下图显示了我的firebase结构:

enter image description here

这是我的代码: SignupClient.java

      signupClient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String cemail = clie_email.getText().toString().trim();
            final String cpassword = clie_password.getText().toString().trim();

            String ccpassword = clie_cpassword.getText().toString().trim();

            final String cfname = clie_firstname.getText().toString().trim();
            final String clname = clie_lastname.getText().toString().trim();
            final String cbday = clie_birthday.getText().toString().trim();
            final String ccountry = clie_country.getSelectedItem().toString();
            final String cmobile = clie_mobile.getText().toString().trim();

            auth.createUserWithEmailAndPassword(cemail, cpassword)
                    .addOnCompleteListener(_5_SignupClient.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            Toast.makeText(_5_SignupClient.this, "createUserWithEmail: onComplete" + task.isSuccessful(), Toast.LENGTH_LONG).show();

                            if (!task.isSuccessful()){
                                Toast.makeText(_5_SignupClient.this, "Authentication Failed" + task.getException(),
                                        Toast.LENGTH_LONG).show();
                            }
                            else {
                                AccountInfo accountInfo = new AccountInfo(cfname, clname, cemail, cpassword, cbday, ccountry, cmobile);
                                mDatabaseReference.child("client").push().setValue(accountInfo);
                                startActivity(new Intent(_5_SignupClient.this, _7_ViewClient.class));
                                finish();
                            }
                        }
                    });
        }
    });

LoginClient.java

         loginClient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String clie_unameemail = clie_emailuname.getText().toString();
            final String clie_pass = clie_password.getText().toString();

            if(TextUtils.isEmpty(clie_unameemail)){
                Toast.makeText(getApplicationContext(), "Field cannot be empty", Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(clie_pass)){
                Toast.makeText(getApplicationContext(), "Field cannot be empty", Toast.LENGTH_LONG).show();
                return;
            }



            auth.signInWithEmailAndPassword(clie_unameemail, clie_pass)
                    .addOnCompleteListener(_3_LoginClient.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                           mAuthListener = new FirebaseAuth.AuthStateListener() {
                                @Override
                                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                                    FirebaseUser user = firebaseAuth.getCurrentUser();
                                    if (user != null) {
                                        ref = FirebaseDatabase.getInstance().getReference().child("client");

                                        ref.addListenerForSingleValueEvent(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(DataSnapshot dataSnapshot) {
                                                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                                                    if(FirebaseAuth.getInstance().getCurrentUser().getUid().equals(snapshot.getKey())){
                                                        startActivity(new Intent(_3_LoginClient.this, _7_ViewClient.class));
                                                    }
                                                }
                                             //   startActivity(new Intent(_3_LoginClient.this, Normal_memberActivity.class));
                                            }

                                            @Override
                                            public void onCancelled(DatabaseError databaseError) {

                                            }
                                        });

                                    } else {
                                        // User is signed out
                                    }
                                    // ...
                                }
                            };

                            if (!task.isSuccessful()) {
                                // there was an error
                                if (clie_pass.length() < 8) {
                                    clie_password.setError(getString(R.string.minimum_password));
                                } else {
                                    Toast.makeText(_3_LoginClient.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                }
                            } else {
                                Toast.makeText(_3_LoginClient.this, "Successfully Registered", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(_3_LoginClient.this, _7_ViewClient.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });
        }
    }); 

我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:0)

在您的数据库上应该有一个字段,就像我们说 USER_TYPE 一样。注册用户时发送其USER_TYPE。假设您将用户注册为 CLIENT ,然后将inser db值 USER_TYPE =&#34; CLIENT&#34; ,如果将其作为事件成员注册,则为inser db值 USER_TYPE =&#34; EVENT&#34; 现在,当您登录后,根据 USER_TYPE检查其USER_TYPE 重定向