diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index d0a4485..2f29b24 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -232,37 +232,28 @@ public class LoginActivity extends AppCompatActivity { String oauthUrl = "/api/v1/users/token"; try { String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null); - runOnUiThread(() -> { - JSONObject resobjLogin; - try { - resobjLogin = new JSONObject(responseLogin); - String token = resobjLogin.getString("access_token"); - String refresh_token = resobjLogin.getString("refresh_token"); - editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); - editor.putString(Helper.PREF_INSTANCE, host); - editor.apply(); - //Update the account with the token; - new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } catch (JSONException e) { - e.printStackTrace(); - runOnUiThread(() -> connectionButton.setEnabled(true)); - } - }); + proceedLogin(responseLogin, host); } catch (final Exception e) { - e.printStackTrace(); - runOnUiThread(() -> { - connectionButton.setEnabled(true); - String message; - if (e.getLocalizedMessage() != null && e.getLocalizedMessage().trim().length() > 0) - message = e.getLocalizedMessage(); - else if (e.getMessage() != null && e.getMessage().trim().length() > 0) - message = e.getMessage(); - else - message = getString(R.string.client_error); - Toasty.error(LoginActivity.this, message, Toast.LENGTH_LONG).show(); - }); + parameters.remove("username"); + parameters.put("username", login_uid.getText().toString().trim().toLowerCase()); + try { + String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null); + proceedLogin(responseLogin, host); + }catch (final Exception e2) { + e2.printStackTrace(); + runOnUiThread(() -> { + connectionButton.setEnabled(true); + String message; + if (e2.getLocalizedMessage() != null && e2.getLocalizedMessage().trim().length() > 0) + message = e2.getLocalizedMessage(); + else if (e2.getMessage() != null && e2.getMessage().trim().length() > 0) + message = e2.getMessage(); + else + message = getString(R.string.client_error); + Toasty.error(LoginActivity.this, message, Toast.LENGTH_LONG).show(); + }); + } } - } catch (JSONException e) { e.printStackTrace(); e.printStackTrace(); @@ -288,6 +279,27 @@ public class LoginActivity extends AppCompatActivity { }); } + private void proceedLogin(String responseLogin, String host){ + runOnUiThread(() -> { + JSONObject resobjLogin; + try { + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + resobjLogin = new JSONObject(responseLogin); + String token = resobjLogin.getString("access_token"); + String refresh_token = resobjLogin.getString("refresh_token"); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); + editor.putString(Helper.PREF_INSTANCE, host); + editor.apply(); + //Update the account with the token; + new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } catch (JSONException e) { + e.printStackTrace(); + runOnUiThread(() -> connectionButton.setEnabled(true)); + } + }); + } + @Override protected void onResume() { super.onResume();