Improved login error messages

This commit is contained in:
Balazs Toldi 2023-07-28 07:59:41 +02:00
parent 497f6c79d2
commit df81866821
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
2 changed files with 24 additions and 14 deletions

View File

@ -21,11 +21,11 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.inject.Inject; import javax.inject.Inject;
@ -158,14 +158,16 @@ public class LoginActivity extends BaseActivity {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
progressBar.setVisibility(ProgressBar.GONE); progressBar.setVisibility(ProgressBar.GONE);
loginButton.setEnabled(true); loginButton.setEnabled(true);
if (response.isSuccessful()) {
String accountResponse = response.body(); String accountResponse = response.body();
if (accountResponse == null) { if (accountResponse == null) {
Log.e("LoginActivity", "Account response is null"); Log.e("LoginActivity", "Account response is null");
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, R.string.invalid_response, Toast.LENGTH_SHORT).show();
//Handle error //Handle error
return; return;
} }
if (response.isSuccessful()) {
try { try {
JSONObject responseJSON = new JSONObject(accountResponse); JSONObject responseJSON = new JSONObject(accountResponse);
String accessToken = responseJSON.getString("jwt"); String accessToken = responseJSON.getString("jwt");
@ -206,15 +208,21 @@ public class LoginActivity extends BaseActivity {
} }
try { try {
JSONObject responseObject = new JSONObject(accountResponse); String errorBody = response.errorBody().string();
JSONObject responseObject = new JSONObject(errorBody.trim());
if (responseObject.has("error")) { if (responseObject.has("error")) {
if (responseObject.getString("error").equals("incorrect_login")) {
Toast.makeText(LoginActivity.this, R.string.invalid_username_or_password, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Error:" + responseObject.getString("error"), Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, "Error:" + responseObject.getString("error"), Toast.LENGTH_SHORT).show();
} }
else { } else {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
} }
} catch (JSONException e) { } catch (JSONException e) {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
} }
Log.e("LoginActivity", "Failed to get access token: " + response.code() + " " + response.message() + " " + response.errorBody()); Log.e("LoginActivity", "Failed to get access token: " + response.code() + " " + response.message() + " " + response.errorBody());

View File

@ -149,6 +149,8 @@
<string name="online_subscribers_number_detail">Online: %1$,d</string> <string name="online_subscribers_number_detail">Online: %1$,d</string>
<string name="cannot_fetch_community_info">Cannot fetch community info</string> <string name="cannot_fetch_community_info">Cannot fetch community info</string>
<string name="cannot_fetch_user_info">Cannot fetch user info</string> <string name="cannot_fetch_user_info">Cannot fetch user info</string>
<string name="invalid_response">Invalid response from the server</string>
<string name="invalid_username_or_password">Invalid username or password</string>
<string name="cannot_fetch_sidebar">Cannot fetch sidebar</string> <string name="cannot_fetch_sidebar">Cannot fetch sidebar</string>
<string name="cannot_fetch_multireddit">Cannot fetch multireddit info</string> <string name="cannot_fetch_multireddit">Cannot fetch multireddit info</string>