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:
Graham Jones
2021-12-12 22:49:30 +00:00
parent 32ab7d4229
commit 20f79264fe
3 changed files with 49 additions and 11 deletions

View File

@@ -12,6 +12,7 @@ import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView;
public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface { public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
private String TAG = "AuthenticateActivity"; private String TAG = "AuthenticateActivity";
@@ -31,6 +32,11 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
cancelBtn.setOnClickListener(onCancel); cancelBtn.setOnClickListener(onCancel);
Button OKBtn = (Button) findViewById(R.id.OKBtn); Button OKBtn = (Button) findViewById(R.id.OKBtn);
OKBtn.setOnClickListener(onOK); 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); mUnameEt = (EditText) findViewById(R.id.username);
mPasswdEt = (EditText) findViewById(R.id.password); mPasswdEt = (EditText) findViewById(R.id.password);
@@ -40,15 +46,15 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
switchUi(); updateUi();
} }
public void authCallback(boolean authSuccess, String tokenStr) { public void authCallback(boolean authSuccess, String tokenStr) {
Log.v(TAG,"authCallback"); Log.v(TAG,"authCallback");
switchUi(); updateUi();
} }
private void switchUi() { private void updateUi() {
SharedPreferences prefs; SharedPreferences prefs;
String storedAuthToken; String storedAuthToken;
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui); 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"); Log.v(TAG, "Already Logged in - showing Log Out prompt");
loginLl.setVisibility(View.GONE); loginLl.setVisibility(View.GONE);
logoutLl.setVisibility(View.VISIBLE); 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();
}
};
} }

View File

@@ -51,9 +51,16 @@ public class WebApiConnection {
new Response.Listener<String>() { new Response.Listener<String>() {
@Override @Override
public void onResponse(String response) { public void onResponse(String response) {
String tokenStr;
Log.v(TAG, "Response is: " + response); 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); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.edit().putString("webApiAuthToken", response).commit(); prefs.edit().putString("webApiAuthToken", tokenStr).commit();
mAuthCallback.authCallback(true, response); mAuthCallback.authCallback(true, response);
} }
}, },
@@ -82,4 +89,23 @@ public class WebApiConnection {
return (true); 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!");
}
} }

View File

@@ -4,16 +4,10 @@
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"> android:orientation="vertical">
<TextView
android:id="@+id/versionTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android OpenSeizureDetector Version x.xx" />
<ImageView <ImageView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="64dp" android:layout_height="64dp"
android:background="#FFFFBB33" android:background="#FFFFFF"
android:contentDescription="@string/app_name" android:contentDescription="@string/app_name"
android:scaleType="center" android:scaleType="center"
android:src="@drawable/star_of_life_24x24" /> android:src="@drawable/star_of_life_24x24" />