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);
}
}