1

I have a login screen with email and password fields. Once the user is validated, an access token is given to the user and updated appropriately.

However, when the app crashes and is restarted, the token is invalidated because the email and password are lost.

How can I recover the email and password in an onResume()?

     private class UserLoginTask extends AsyncTask<Void, Void, String> {

        private final String lurl;
        private final String mEmail;
        private final String mPassword;

        UserLoginTask(String url, String email, String password) {
            lurl = url;
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected String doInBackground(Void... params) {


            try {
                WebConnection conn = new WebConnection(lurl, getApplicationContext());
                conn.addValuePair("username", mEmail);
                Log.v("mEmail", mEmail);
                conn.addValuePair("password", mPassword);
                Log.v("mPassword", mPassword);

                String response = conn.connect();


                return response;


            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;

}

        @Override
        protected void onPostExecute(final String response) {
            mAuthTask = null;
            showProgress(false);


            if(response != null) {

                SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                editor.putString("email", mEmail);
                editor.putString("password", mPassword);
                editor.apply();
            }

//NEED TO GET RESPONSE FROM SERVER IF LOGIN BASED ON PREFS WAS SUCCESSFUL
            if (success) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            } else {
                mPasswordView.setError(getString(R.string.error_incorrect_password));
                mPasswordView.requestFocus();
            }
        }





     @Override
        protected void onResume() {
            super.onResume();


            Context context = getApplicationContext();
            SharedPreferences prefs;
            prefs = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
            String token = prefs.getString("token", null);

            //NEED TO GET mEmail and mPassword values from other activity
            if (mEmail == null) //Check if it isn't already set
                mEmail = prefs.getString("email", null);
            if (mPassword == null)
                mPassword = prefs.getString("password", null);
if(token == null || token.isEmpty()){
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }

    }
JohnWilliams
  • 139
  • 1
  • 13
  • If I get you right, you receive an access token after a successful login procedure. What's wrong with storing this token (for example inside SharedPreferences) rather than the login credentials / why should it be invalidated? – localhorst Jan 04 '16 at 20:25
  • @localhorst The token is invalidated when the app crashes. I just want to grab the email and password if the app does crash and save it so the login can be performed again. Please see my updated code. Thanks. – JohnWilliams Jan 04 '16 at 21:14

3 Answers3

0

If your application go crashed then all the data will be lost , but if you still want to persist you can either use sqlite or shared preferences with proper encryption scheme, since username and password sensitive data.

0

One way is to save the username and password in some SharedPreferences, before a login-try is made. Please see the documentation here: Google Devs. A simple saving would be:

SharedPreferences prefs = getSharedPreferences("MY_PREFERENCES", MODE_PRIVATE);
        prefs.edit().putString("USERNAME", theUsername).commit();
        prefs.edit().putString("PASSWORD", thePassword).commit();
Carsten Drösser
  • 496
  • 1
  • 4
  • 16
  • Please take a look at my updated code that used a variation of your answer and Thomas Lü's answer. I commented sections of code that I would like suggestions on. Thanks. – JohnWilliams Jan 04 '16 at 21:09
0

Store it in SharedPreferences in your onPostExecute:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("email", mEmail);
editor.putString("password", mPassword);
editor.apply();

Read it in your onResume:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
if (mEmail == null) //Check if it isn't already set
  mEmail = prefs.getString("email", null);
if (mPassword == null)
  mPassword = prefs.getString("password", null);

//Do your stuff here, but check if the Strings are null before

The better way is of course to get rid of the crashes. It's not a good sign if you're sure that your app will crash, especially on the first screen.

Thomas
  • 483
  • 7
  • 22
  • How can I reference mEmail in another class? So mEmail and mPassword are established in the class that contains UserLoginTask and the onResume is in the MainActivity. Of course the MainActivity will not know about the mEmail and mPassword from another class. How can I make those variables know with correct values? (Yes, I am new to Android development, btw) Also, I know it's not a good sign if the app crashes on the first screen, but I need to ensure that user can be logged in if they have the token so it is a lot of error handling. – JohnWilliams Jan 04 '16 at 20:52
  • @JohnWilliams You can pass them together with the intent in your onResume. Example: intent.putString("email", mEmail); On your onCreate of your LoginActivity you then get the String with Intent intent = getIntent(); String email = intent.getStringExtra("email"); Then you have the value of mEmail stored in email in your LoginActivity. It would be nice if you could mark the answer that helped your the most as most helpful answer. Cheers – Thomas Jan 05 '16 at 09:11
  • @JohnWilliams Here is more info about intent: http://stackoverflow.com/a/4233898/4026792. It's btw always better to store the token than to store the user password. – Thomas Jan 05 '16 at 09:16
  • I am not following your example with the intent. The onResume is in a different class then the onPostExecute. onResume is in a MainActivity and onPostExecute is in a LoginActivity. putString() cannot be applied to an intent. I looked at the link..so should I use bundle.putString() instead? – JohnWilliams Jan 05 '16 at 17:59
  • Sorry, I wasn't on my PC when I wrote this and had no chance to test it. I meant intent.putExtra("email", mEmail); instead of putString. Then you could get it via the above method: mEmail= getIntent().getStringExtra("email"). See the provided link for more examples. – Thomas Jan 05 '16 at 18:50