samedi 19 décembre 2020

Why my checkbox remember me made with the Paper library is failing to function?

I have been trying to code in java in android studio to make my application remember me with https://github.com/pilgr/Paper. I get logged even when I don't voluntarily log out. Here is the code of the activities involved. Please help me to know how to solve this issue.

package com.example.spree;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.spree.Models.Users;
import com.example.spree.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.drawable.CheckBoxDrawable;
import com.rey.material.widget.CheckBox;

import io.paperdb.Book;
import io.paperdb.Paper;

public class LoginActivity extends AppCompatActivity {

    private EditText phoneNumber, userPassword;
    private Button loginButton;
    private ProgressDialog loadingDialog;
    private String parentDBName = "Users";

    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        phoneNumber = findViewById(R.id.login_phone_number_input);
        userPassword = findViewById(R.id.login_password_input);

        loginButton = findViewById(R.id.login_btn);
        loadingDialog = new ProgressDialog(this);

        checkBox = (CheckBox) findViewById(R.id.remember_me_chkb);
        Paper.init(this);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginUser();
            }
        });
        
        
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });


    }

    private void loginUser() {

        String phone = phoneNumber.getText().toString();
        String password = userPassword.getText().toString();

         if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "Please, enter your mobile number.", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please, enter your password.", Toast.LENGTH_SHORT).show();
        } else {

             loadingDialog.setTitle("Login Account");
             loadingDialog.setMessage("Please, wait while we are checking the credentials.");
             loadingDialog.setCanceledOnTouchOutside(false);
             loadingDialog.show();
             
             allowAccessToUser(phone, password);

         }

    }

    private void allowAccessToUser(final String phone, final String password) {
        
        

        if(checkBox.isChecked()){
            Paper.book().write(Prevalent.userPassKey, phone);
            Paper.book().write(Prevalent.userPassKey, password);

        }

        final DatabaseReference myRef;
        myRef = FirebaseDatabase.getInstance().getReference();


        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.child(parentDBName).child(phone).exists()){
                    Users userData = snapshot.child(parentDBName).child(phone).getValue(Users.class);

                    if(userData.getPhone().equals(phone)){
                        if(userData.getPassword().equals(password)){

                            Toast.makeText(LoginActivity.this, "Logged in successfully!", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();

                            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                            startActivity(intent);
                        }

                        else
                        {
                            Toast.makeText(LoginActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();
                        }
                    }
                }
                else
                {
                    Toast.makeText(LoginActivity.this, "Account with this "+phone+ " doesn't exist", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                }

            }

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

            }
        });
    }
}

MainActivity

    package com.example.spree;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.spree.Models.Users;
import com.example.spree.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import io.paperdb.Paper;

public class MainActivity extends AppCompatActivity {

    private Button signUp, signIn;
    private ProgressDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        signIn = findViewById(R.id.main_login_btn);
        signUp = findViewById(R.id.main_sign_up_btn);

        Paper.init(this);

        loadingDialog = new ProgressDialog(this);

        signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


        String phone = Paper.book().read(Prevalent.userPhoneKey);
        String password = Paper.book().read(Prevalent.userPassKey);


        if (phone!="" && password!=""){

            if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)){
                allowAccess(phone, password);


                loadingDialog.setTitle("Already logged in");
                loadingDialog.setMessage("Please, wait a little while...");
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
            }
        }
    }

    private void allowAccess(final String phone, final String password) {


        final DatabaseReference myRef;
        myRef = FirebaseDatabase.getInstance().getReference();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.child("Users").child(phone).exists()){
                    Users userData = snapshot.child("Users").child(phone).getValue(Users.class);

                    if(userData.getPhone().equals(phone)){
                        if(userData.getPassword().equals(password)){

                            Toast.makeText(MainActivity.this, "Already logged in!", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();

                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(intent);
                        }

                        else
                        {
                            Toast.makeText(MainActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();
                        }
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Account with this "+phone+ " doesn't exist", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                }

            }

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

            }
        });
    }
}



Aucun commentaire:

Enregistrer un commentaire