'How to login with offline mode when a valid user login online

I am developing an app. My goal is that user can login online and offline both, but for the offline mode user should login online enters their credentials (userid and password) and save the user's credentials with the help of checkbox.

To check the credentials I used a webservice which will check that user id and password whether valid or not. I am able to consume webservice, and check whether user is valid or not. I'm able to save the userid and encrypted password in order to compare the inputted password with stored password in the database.

The problem I am facing is that when the userid or password is not correct it still saves the credentials in sqlite database. When I'm in offline mode I can login with incorrect password! I only want credentials to be saved when user is authenticated.

Below is my code. I don't get it where am I going wrong to call offline method. Function for Login Button:

    login.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      // preventing double, using threshold of 1000 ms
      if (SystemClock.elapsedRealtime() - lastClickTimeBtnLogin < Utilities.BTN_DOUBLE_CLICK_STOP_TIME) {
        return;
      }
      lastClickTimeBtnLogin = SystemClock.elapsedRealtime();
    
      // Login Logic
      try {
        String mail = email.getText().toString().trim();
        String pwd = password.getText().toString().trim();
        if (iCD.checkMobileInternetConn()) {
          if (isLocationEnabled) {                       
            if (mail.equals("")) {
              Toast.makeText(getApplicationContext(), "Please enter UserName", Toast.LENGTH_SHORT).show();
            } else if (pwd.equals("")) {
              Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_LONG).show();
            }
          else {
            if (offlineMode.isChecked()) {
              boolean i =global.getDb().addUser(mail,pwd);
              if (i== true){
                Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_LONG).show();
              } else {
                Toast.makeText(getApplicationContext(), "Not inserted", Toast.LENGTH_LONG).show();
              }
            }
            
            global.setUserName(email.getText().toString().trim());
            srv = new GenericServiceCalling(mContext, cb);
            JSONObject obj = new JSONObject();
            obj.put("userID", mail);
            obj.put("password", pwd);
            JSONObject mainObj = new JSONObject();
            mainObj.put("value", obj);
            showCustomLoader();
            srv.execute(Utilities.LOGIN, mainObj.toString());
          }
        } else {
          Toast.makeText(getApplicationContext(), "Enable location", Toast.LENGTH_LONG).show();
        }
      }
    else {
      offlineLogin();
    }
  }
  catch (JSONException e) {
    e.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();                          
  }
 }
});

Check response code==200:


     @Override
        public void onTaskComplete(String result) {
            mCustomLoader.dismiss();
            Log.e(TAG, "Response from url: " + result);
            String[] resultResponse = result.split("\\|", 2);
            int responseCode = Integer.valueOf(resultResponse[0].trim());
            String responseBody = resultResponse[1].trim();
    
    
            if (responseCode == 200) {
                try {
                    JSONObject jsonObj = new JSONObject(responseBody);
                    if (jsonObj.has("LoginResult")){
                        parseLoginResult(jsonObj);
    
                    }
    
                    else {
    
    
                    }
    
    
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
    
                }
    
            }
            else {
                if(responseCode == Utilities.EXCEPTION_RESPONSE_CODE){
                    showCustomAlertOneBtn("Alert", "Something wrong Response Code: "+responseCode+" = "+responseBody );
                }
                else{
                    showCustomAlertOneBtn("Alert", "Internal server error. ResponseCode : "+responseCode +" Message "+ responseBody );
                }
    
            }
    
        }
     private void parseLoginResult(JSONObject object) {
            try {
                if(object.getString("LoginResult").equals("null")){
                    mCustomLoader.dismiss();
                    Log.e(TAG, "Authenticaion Failed" );
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Authenticaion Failed",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }else {
                  
                  if(object.getJSONObject("LoginResult").getString("responseCode").equals("200")) {
    
                        Intent intent = new Intent(MainActivity.this, list.class);
                        startActivity(intent);
                    }
    
                    else{                      
                        Toast.makeText(getApplicationContext(), object.getJSONObject("LoginResult").getString("responseMessage"),
                                Toast.LENGTH_LONG).show();
                    }
                }
            }catch (final JSONException e) {
                mCustomLoader.dismiss();
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });
    
            }catch (Exception e) {
                mCustomLoader.dismiss();
                Log.e(TAG, "Exception Occur: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Exception Occur: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });
    
            }
    
        }

offline login Method:
 

    public void offlineLogin(){
            iCD.hideCustomAlert();
    
            if (isLocationEnabled) {
                String mail = email.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                String pwd1 = md5(pwd);
    
                if (mail.equals("")) {
                    Toast.makeText(getApplicationContext(), "Please enter UserName", Toast.LENGTH_SHORT).show();
                } else if (pwd.equals("")) {
                    Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_LONG).show();
                }
                else{
    
                        Boolean res = global.getDb().checkUser(mail, pwd1);
    //                Boolean res = helper.checkUser(mail, pwd1);
    
                    if(res == true) {
                        Intent intent = new Intent(MainActivity.this, list.class);
                        startActivity(intent);
    
                        Toast.makeText(MainActivity.this, "Successfully Logged In", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Login Error",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            } else {
    Toast.makeText(MainActivity.this, "enabled Location",
                                    Toast.LENGTH_SHORT).show();
               
            }
        }




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source