Fixed authentication UI - it will now save the token to persistent storage and the logout button deletes the stored token
This commit is contained in:
@@ -12,6 +12,7 @@ import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
|
||||
private String TAG = "AuthenticateActivity";
|
||||
@@ -31,6 +32,11 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
cancelBtn.setOnClickListener(onCancel);
|
||||
Button OKBtn = (Button) findViewById(R.id.OKBtn);
|
||||
OKBtn.setOnClickListener(onOK);
|
||||
Button logoutCancelBtn =
|
||||
(Button) findViewById(R.id.logoutCancelBtn);
|
||||
logoutCancelBtn.setOnClickListener(onCancel);
|
||||
Button logoutBtn = (Button)findViewById(R.id.logoutBtn);
|
||||
logoutBtn.setOnClickListener(onLogout);
|
||||
|
||||
mUnameEt = (EditText) findViewById(R.id.username);
|
||||
mPasswdEt = (EditText) findViewById(R.id.password);
|
||||
@@ -40,15 +46,15 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
switchUi();
|
||||
updateUi();
|
||||
}
|
||||
|
||||
public void authCallback(boolean authSuccess, String tokenStr) {
|
||||
Log.v(TAG,"authCallback");
|
||||
switchUi();
|
||||
updateUi();
|
||||
}
|
||||
|
||||
private void switchUi() {
|
||||
private void updateUi() {
|
||||
SharedPreferences prefs;
|
||||
String storedAuthToken;
|
||||
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui);
|
||||
@@ -67,6 +73,8 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
Log.v(TAG, "Already Logged in - showing Log Out prompt");
|
||||
loginLl.setVisibility(View.GONE);
|
||||
logoutLl.setVisibility(View.VISIBLE);
|
||||
TextView tv = (TextView)findViewById(R.id.tokenTv);
|
||||
tv.setText("Logged in with Token:"+storedAuthToken);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -95,5 +103,15 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onLogout =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onLogout");
|
||||
//m_status=false;
|
||||
mWac.logout();
|
||||
updateUi();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -51,9 +51,16 @@ public class WebApiConnection {
|
||||
new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
String tokenStr;
|
||||
Log.v(TAG, "Response is: " + response);
|
||||
try {
|
||||
JSONObject jo = new JSONObject(response);
|
||||
tokenStr = jo.getString("token");
|
||||
} catch (JSONException e) {
|
||||
tokenStr = "Error Parsing Rsponse";
|
||||
}
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
prefs.edit().putString("webApiAuthToken", response).commit();
|
||||
prefs.edit().putString("webApiAuthToken", tokenStr).commit();
|
||||
mAuthCallback.authCallback(true, response);
|
||||
}
|
||||
},
|
||||
@@ -82,4 +89,23 @@ public class WebApiConnection {
|
||||
return (true);
|
||||
}
|
||||
|
||||
// Remove the stored token so future calls are not authemticated.
|
||||
public void logout() {
|
||||
Log.v(TAG, "logout()");
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
prefs.edit().putString("webApiAuthToken", null).commit();
|
||||
}
|
||||
|
||||
|
||||
// Create a new event in the remote database, based on the provided parameters.
|
||||
public boolean createEvent() {
|
||||
Log.v(TAG,"createEvent() - FIXME - This does not do anything!");
|
||||
|
||||
}
|
||||
|
||||
public boolean createDatapoint() {
|
||||
Log.v(TAG,"createDatapoint() - FIXME - This does not do anything!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user