checking if a user is in a specific child for login firebase












0














I am making an android application using Firebase database and I want to check that the user is not in registered as an "Association" so I am checking if he belongs to the child "Association".
The method userLogin is supposed to not log in the user if he is under the child "Associations" and log in him otherwise.
However, it is not working and the user is logged in even if he is under "Associations"



    private void userLogin() {
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();



mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {

FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String RegisteredUserID = currentUser.getUid();

DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations").child(RegisteredUserID);

jLoginDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if(dataSnapshot.exists()) {
Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
}
else
{
finish();
Intent intent = new Intent(SignInDonor.this, homedonor.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}



}
@Override
public void onCancelled(DatabaseError databaseError) {

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









share|improve this question



























    0














    I am making an android application using Firebase database and I want to check that the user is not in registered as an "Association" so I am checking if he belongs to the child "Association".
    The method userLogin is supposed to not log in the user if he is under the child "Associations" and log in him otherwise.
    However, it is not working and the user is logged in even if he is under "Associations"



        private void userLogin() {
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();



    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    progressBar.setVisibility(View.GONE);
    if (task.isSuccessful()) {

    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    String RegisteredUserID = currentUser.getUid();

    DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations").child(RegisteredUserID);

    jLoginDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    if(dataSnapshot.exists()) {
    Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
    }
    else
    {
    finish();
    Intent intent = new Intent(SignInDonor.this, homedonor.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    }



    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

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









    share|improve this question

























      0












      0








      0







      I am making an android application using Firebase database and I want to check that the user is not in registered as an "Association" so I am checking if he belongs to the child "Association".
      The method userLogin is supposed to not log in the user if he is under the child "Associations" and log in him otherwise.
      However, it is not working and the user is logged in even if he is under "Associations"



          private void userLogin() {
      String email = editTextEmail.getText().toString().trim();
      String password = editTextPassword.getText().toString().trim();



      mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
      progressBar.setVisibility(View.GONE);
      if (task.isSuccessful()) {

      FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
      String RegisteredUserID = currentUser.getUid();

      DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations").child(RegisteredUserID);

      jLoginDatabase.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {

      if(dataSnapshot.exists()) {
      Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
      }
      else
      {
      finish();
      Intent intent = new Intent(SignInDonor.this, homedonor.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);
      }



      }
      @Override
      public void onCancelled(DatabaseError databaseError) {

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









      share|improve this question













      I am making an android application using Firebase database and I want to check that the user is not in registered as an "Association" so I am checking if he belongs to the child "Association".
      The method userLogin is supposed to not log in the user if he is under the child "Associations" and log in him otherwise.
      However, it is not working and the user is logged in even if he is under "Associations"



          private void userLogin() {
      String email = editTextEmail.getText().toString().trim();
      String password = editTextPassword.getText().toString().trim();



      mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
      progressBar.setVisibility(View.GONE);
      if (task.isSuccessful()) {

      FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
      String RegisteredUserID = currentUser.getUid();

      DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations").child(RegisteredUserID);

      jLoginDatabase.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {

      if(dataSnapshot.exists()) {
      Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
      }
      else
      {
      finish();
      Intent intent = new Intent(SignInDonor.this, homedonor.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);
      }



      }
      @Override
      public void onCancelled(DatabaseError databaseError) {

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






      android firebase login






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 0:10









      user6465283

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I did not tried this, but it should work, first, after you log in with a user and enters inside task.isSuccessful , you can retrieve the current logged in user with task.getResult().getUser().getUid(). Then just loop inside Associations and get each user key (I assume that Associations has userIDs inside as nodes with a certain value), then compare if the current logged in user is equal to one inside that node, if matchs it will pop up your Toast, if not you will be redirected.



          Try this



          public void onComplete(@NonNull Task<AuthResult> task) {
          progressBar.setVisibility(View.GONE);
          if (task.isSuccessful()) {

          DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations");

          jLoginDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot snapshot : dataSnapshot.getChildren()){

          if(snapshot.getKey().equals(task.getResult().getUser().getUid()) {
          Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
          }
          else
          {
          finish();
          Intent intent = new Intent(SignInDonor.this, homedonor.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }

          }


          I used addListenerForSingleValueEvent because we only need to loop once at the reference and not keep listening for data






          share|improve this answer























          • What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
            – user6465283
            Dec 28 '18 at 12:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952254%2fchecking-if-a-user-is-in-a-specific-child-for-login-firebase%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I did not tried this, but it should work, first, after you log in with a user and enters inside task.isSuccessful , you can retrieve the current logged in user with task.getResult().getUser().getUid(). Then just loop inside Associations and get each user key (I assume that Associations has userIDs inside as nodes with a certain value), then compare if the current logged in user is equal to one inside that node, if matchs it will pop up your Toast, if not you will be redirected.



          Try this



          public void onComplete(@NonNull Task<AuthResult> task) {
          progressBar.setVisibility(View.GONE);
          if (task.isSuccessful()) {

          DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations");

          jLoginDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot snapshot : dataSnapshot.getChildren()){

          if(snapshot.getKey().equals(task.getResult().getUser().getUid()) {
          Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
          }
          else
          {
          finish();
          Intent intent = new Intent(SignInDonor.this, homedonor.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }

          }


          I used addListenerForSingleValueEvent because we only need to loop once at the reference and not keep listening for data






          share|improve this answer























          • What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
            – user6465283
            Dec 28 '18 at 12:58
















          1














          I did not tried this, but it should work, first, after you log in with a user and enters inside task.isSuccessful , you can retrieve the current logged in user with task.getResult().getUser().getUid(). Then just loop inside Associations and get each user key (I assume that Associations has userIDs inside as nodes with a certain value), then compare if the current logged in user is equal to one inside that node, if matchs it will pop up your Toast, if not you will be redirected.



          Try this



          public void onComplete(@NonNull Task<AuthResult> task) {
          progressBar.setVisibility(View.GONE);
          if (task.isSuccessful()) {

          DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations");

          jLoginDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot snapshot : dataSnapshot.getChildren()){

          if(snapshot.getKey().equals(task.getResult().getUser().getUid()) {
          Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
          }
          else
          {
          finish();
          Intent intent = new Intent(SignInDonor.this, homedonor.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }

          }


          I used addListenerForSingleValueEvent because we only need to loop once at the reference and not keep listening for data






          share|improve this answer























          • What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
            – user6465283
            Dec 28 '18 at 12:58














          1












          1








          1






          I did not tried this, but it should work, first, after you log in with a user and enters inside task.isSuccessful , you can retrieve the current logged in user with task.getResult().getUser().getUid(). Then just loop inside Associations and get each user key (I assume that Associations has userIDs inside as nodes with a certain value), then compare if the current logged in user is equal to one inside that node, if matchs it will pop up your Toast, if not you will be redirected.



          Try this



          public void onComplete(@NonNull Task<AuthResult> task) {
          progressBar.setVisibility(View.GONE);
          if (task.isSuccessful()) {

          DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations");

          jLoginDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot snapshot : dataSnapshot.getChildren()){

          if(snapshot.getKey().equals(task.getResult().getUser().getUid()) {
          Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
          }
          else
          {
          finish();
          Intent intent = new Intent(SignInDonor.this, homedonor.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }

          }


          I used addListenerForSingleValueEvent because we only need to loop once at the reference and not keep listening for data






          share|improve this answer














          I did not tried this, but it should work, first, after you log in with a user and enters inside task.isSuccessful , you can retrieve the current logged in user with task.getResult().getUser().getUid(). Then just loop inside Associations and get each user key (I assume that Associations has userIDs inside as nodes with a certain value), then compare if the current logged in user is equal to one inside that node, if matchs it will pop up your Toast, if not you will be redirected.



          Try this



          public void onComplete(@NonNull Task<AuthResult> task) {
          progressBar.setVisibility(View.GONE);
          if (task.isSuccessful()) {

          DatabaseReference jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Associations");

          jLoginDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot snapshot : dataSnapshot.getChildren()){

          if(snapshot.getKey().equals(task.getResult().getUser().getUid()) {
          Toast.makeText(getApplicationContext(), "You are not registered", Toast.LENGTH_SHORT).show();
          }
          else
          {
          finish();
          Intent intent = new Intent(SignInDonor.this, homedonor.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }

          }


          I used addListenerForSingleValueEvent because we only need to loop once at the reference and not keep listening for data







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 0:45

























          answered Dec 28 '18 at 0:38









          Gastón Saillén

          3,2374932




          3,2374932












          • What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
            – user6465283
            Dec 28 '18 at 12:58


















          • What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
            – user6465283
            Dec 28 '18 at 12:58
















          What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
          – user6465283
          Dec 28 '18 at 12:58




          What you assumed is true. However I tried your solution and I still can login with a user that belongs to "Associations".
          – user6465283
          Dec 28 '18 at 12:58


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952254%2fchecking-if-a-user-is-in-a-specific-child-for-login-firebase%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'