Allow login with email (#239)

While the call to obtain a JWT (`api/v3/user/login`) works for username or email, the app was then following that with a call to ` api/v3/user`, which only works with username (https://lemmy.readme.io/reference/get_user). This would cause the login attempt to fail with the error reported on #235.

These changes skip the call to `api/v3/user`, and instead retrieves the user information from the call to `api/v3/site`, that was already being performed.

Fixes #235.

Question: should the label of the username field be updated to "Username / Email" or "Username or Email"? (screenshot)

Reviewed-on: https://codeberg.org/Bazsalanszky/Eternity/pulls/239
Reviewed-by: Bazsalanszky <bazsalanszky@noreply.codeberg.org>
Co-authored-by: tinsukE <tinsuke@noreply.codeberg.org>
Co-committed-by: tinsukE <tinsuke@noreply.codeberg.org>
This commit is contained in:
tinsukE 2024-01-06 20:18:32 +00:00 committed by Bazsalanszky
parent 3d5c936fa3
commit 0662173045
9 changed files with 163 additions and 204 deletions

View File

@ -1,99 +0,0 @@
package eu.toldi.infinityforlemmy;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import org.json.JSONException;
import org.json.JSONObject;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.utils.LemmyUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
public class FetchMyInfo {
public static void fetchAccountInfo(final Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase,
String username,String accessToken, final FetchMyInfoListener fetchMyInfoListener) {
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> userInfo = api.userInfo(username,accessToken);
userInfo.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
new ParseAndSaveAccountInfoAsyncTask(response.body(), redditDataRoomDatabase, fetchMyInfoListener).execute();
} else {
fetchMyInfoListener.onFetchMyInfoFailed(false);
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchMyInfoListener.onFetchMyInfoFailed(false);
}
});
}
public interface FetchMyInfoListener {
void onFetchMyInfoSuccess(String name, String display_name,String profileImageUrl, String bannerImageUrl);
void onFetchMyInfoFailed(boolean parseFailed);
}
private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONObject jsonResponse;
private RedditDataRoomDatabase redditDataRoomDatabase;
private FetchMyInfoListener fetchMyInfoListener;
private boolean parseFailed;
private String name;
private String profileImageUrl;
private String bannerImageUrl;
private String display_name;
ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase,
FetchMyInfoListener fetchMyInfoListener) {
try {
jsonResponse = new JSONObject(response);
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.fetchMyInfoListener = fetchMyInfoListener;
parseFailed = false;
} catch (JSONException e) {
fetchMyInfoListener.onFetchMyInfoFailed(true);
}
}
@Override
protected Void doInBackground(Void... voids) {
try {
JSONObject person = jsonResponse.getJSONObject("person_view").getJSONObject("person");
name = LemmyUtils.actorID2FullName(person.getString("actor_id"));
if (!person.isNull("avatar")) {
profileImageUrl = person.getString("avatar");
}
if (!person.isNull("banner")) {
bannerImageUrl = person.getString("banner");
}
display_name = (person.has("display_name")) ? person.getString("display_name") : person.getString("name");
redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl);
} catch (JSONException e) {
parseFailed = true;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (!parseFailed) {
fetchMyInfoListener.onFetchMyInfoSuccess(name,display_name, profileImageUrl, bannerImageUrl);
} else {
fetchMyInfoListener.onFetchMyInfoFailed(true);
}
}
}
}

View File

@ -42,6 +42,7 @@ import eu.toldi.infinityforlemmy.site.FetchSiteInfo;
import eu.toldi.infinityforlemmy.site.SiteInfo;
import eu.toldi.infinityforlemmy.site.SiteStatistics;
import eu.toldi.infinityforlemmy.user.BasicUserInfo;
import eu.toldi.infinityforlemmy.user.MyUserInfo;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
@ -170,7 +171,7 @@ public class InstanceInfoActivity extends BaseActivity {
private void fetchInstanceInfo() {
FetchSiteInfo.fetchSiteInfo(mRetorifitHolder.getRetrofit(), null, new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) {
mLoadingConstraintLayout.setVisibility(View.GONE);
toolbar.setTitle(siteInfo.getName());
if (siteInfo.getSidebar() != null) {
@ -197,7 +198,7 @@ public class InstanceInfoActivity extends BaseActivity {
}
@Override
public void onFetchSiteInfoFailed() {
public void onFetchSiteInfoFailed(boolean parseFailed) {
}
});

View File

@ -39,7 +39,6 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import eu.toldi.infinityforlemmy.FetchMyInfo;
import eu.toldi.infinityforlemmy.Infinity;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
@ -55,6 +54,7 @@ import eu.toldi.infinityforlemmy.lemmyverse.LemmyInstance;
import eu.toldi.infinityforlemmy.lemmyverse.LemmyVerseFetchInstances;
import eu.toldi.infinityforlemmy.site.FetchSiteInfo;
import eu.toldi.infinityforlemmy.site.SiteInfo;
import eu.toldi.infinityforlemmy.user.MyUserInfo;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
import retrofit2.Call;
@ -217,67 +217,58 @@ public class LoginActivity extends BaseActivity {
try {
JSONObject responseJSON = new JSONObject(accountResponse);
String accessToken = responseJSON.getString("jwt");
mRetrofit.setAccessToken(null);
mRetrofit.setAccessToken(accessToken);
FetchMyInfo.fetchAccountInfo(mRetrofit.getRetrofit(), mRedditDataRoomDatabase, username,
accessToken, new FetchMyInfo.FetchMyInfoListener() {
@Override
public void onFetchMyInfoSuccess(String name, String display_name, String profileImageUrl, String bannerImageUrl) {
FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), accessToken, new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
boolean canDownvote = siteInfo.isEnable_downvotes();
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name, display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance, canDownvote, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
String[] version = siteInfo.getVersion().split("\\.");
if (version.length > 0) {
Log.d("SwitchAccount", "Lemmy Version: " + version[0] + "." + version[1]);
int majorVersion = Integer.parseInt(version[0]);
int minorVersion = Integer.parseInt(version[1]);
if (majorVersion > 0 || (majorVersion == 0 && minorVersion >= 19)) {
mRetrofit.setAccessToken(accessToken);
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true).apply();
} else {
mRetrofit.setAccessToken(null);
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, false).apply();
}
}
}
FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), accessToken, new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) {
if (myUserInfo == null) {
finish();
Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onFetchSiteInfoFailed() {
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance,true, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
}
boolean canDownvote = siteInfo.isEnable_downvotes();
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), myUserInfo.getQualifiedName(), myUserInfo.getDisplayName(), accessToken, myUserInfo.getProfileImageUrl(), myUserInfo.getBannerImageUrl(), authCode, finalInstance, canDownvote, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
.putString(SharedPreferencesUtils.ACCOUNT_NAME, display_name)
.putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, name)
.putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,finalInstance)
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply();
mCurrentAccountSharedPreferences.edit()
.putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
.putString(SharedPreferencesUtils.ACCOUNT_NAME, myUserInfo.getDisplayName())
.putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, myUserInfo.getQualifiedName())
.putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,finalInstance)
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, myUserInfo.getProfileImageUrl())
.putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
String[] version = siteInfo.getVersion().split("\\.");
if (version.length > 0) {
Log.d("SwitchAccount", "Lemmy Version: " + version[0] + "." + version[1]);
int majorVersion = Integer.parseInt(version[0]);
int minorVersion = Integer.parseInt(version[1]);
if (majorVersion > 0 || (majorVersion == 0 && minorVersion >= 19)) {
mRetrofit.setAccessToken(accessToken);
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true).apply();
} else {
mRetrofit.setAccessToken(null);
mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, false).apply();
}
}
}
@Override
public void onFetchMyInfoFailed(boolean parseFailed) {
if (parseFailed) {
Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
}
finish();
}
});
@Override
public void onFetchSiteInfoFailed(boolean parseFailed) {
if (parseFailed) {
finish();
Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show();
} else {
progressBar.setVisibility(ProgressBar.GONE);
loginButton.setEnabled(true);
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
}
}
});
} catch (JSONException e) {
throw new RuntimeException(e);
}

View File

@ -128,6 +128,7 @@ import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditData;
import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditViewModel;
import eu.toldi.infinityforlemmy.subscribeduser.SubscribedUserData;
import eu.toldi.infinityforlemmy.user.FetchUserData;
import eu.toldi.infinityforlemmy.user.MyUserInfo;
import eu.toldi.infinityforlemmy.user.UserData;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.CustomThemeSharedPreferencesUtils;
@ -1147,7 +1148,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), mAccessToken, new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) {
String[] version = siteInfo.getVersion().split("\\.");
if (version.length > 0) {
Log.d("MainActvity", "Lemmy Version: " + version[0] + "." + version[1]);
@ -1165,7 +1166,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
}
@Override
public void onFetchSiteInfoFailed() {
public void onFetchSiteInfoFailed(boolean parseFailed) {
}
});

View File

@ -11,6 +11,7 @@ import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.account.Account;
import eu.toldi.infinityforlemmy.site.FetchSiteInfo;
import eu.toldi.infinityforlemmy.site.SiteInfo;
import eu.toldi.infinityforlemmy.user.MyUserInfo;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
public class SwitchAccount {
@ -33,7 +34,7 @@ public class SwitchAccount {
retrofitHolder.setAccessToken(null);
FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() {
@Override
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) {
boolean canDownvote = siteInfo.isEnable_downvotes();
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
String[] version = siteInfo.getVersion().split("\\.");
@ -49,7 +50,7 @@ public class SwitchAccount {
}
@Override
public void onFetchSiteInfoFailed() {
public void onFetchSiteInfoFailed(boolean parseFailed) {
Log.e("SwitchAccount", "Failed to fetch site info");
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
}

View File

@ -1,6 +1,9 @@
package eu.toldi.infinityforlemmy.site;
import org.json.JSONException;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.user.MyUserInfo;
import retrofit2.Retrofit;
public class FetchSiteInfo {
@ -11,24 +14,29 @@ public class FetchSiteInfo {
@Override
public void onResponse(retrofit2.Call<String> call, retrofit2.Response<String> response) {
if (response.isSuccessful()) {
String siteInfoJson = response.body();
SiteInfo siteInfo = SiteInfo.parseSiteInfo(siteInfoJson);
fetchSiteInfoListener.onFetchSiteInfoSuccess(siteInfo);
try {
String siteInfoJson = response.body();
SiteInfo siteInfo = SiteInfo.parseSiteInfo(siteInfoJson);
MyUserInfo myUserInfo = MyUserInfo.parseFromSiteInfo(siteInfoJson);
fetchSiteInfoListener.onFetchSiteInfoSuccess(siteInfo, myUserInfo);
} catch (JSONException e) {
fetchSiteInfoListener.onFetchSiteInfoFailed(true);
}
} else {
fetchSiteInfoListener.onFetchSiteInfoFailed();
fetchSiteInfoListener.onFetchSiteInfoFailed(false);
}
}
@Override
public void onFailure(retrofit2.Call<String> call, Throwable t) {
fetchSiteInfoListener.onFetchSiteInfoFailed();
fetchSiteInfoListener.onFetchSiteInfoFailed(false);
}
}
);
}
public interface FetchSiteInfoListener {
void onFetchSiteInfoSuccess(SiteInfo siteInfo);
void onFetchSiteInfoFailed();
void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo);
void onFetchSiteInfoFailed(boolean parseFailed);
}
}

View File

@ -79,45 +79,40 @@ public class SiteInfo {
return version;
}
public static SiteInfo parseSiteInfo(String siteInfoJson) {
try {
JSONObject siteInfo = new JSONObject(siteInfoJson);
JSONObject siteView = siteInfo.getJSONObject("site_view");
JSONObject site = siteView.getJSONObject("site");
JSONObject localSite = siteView.getJSONObject("local_site");
public static SiteInfo parseSiteInfo(String siteInfoJson) throws JSONException {
JSONObject siteInfo = new JSONObject(siteInfoJson);
JSONObject siteView = siteInfo.getJSONObject("site_view");
JSONObject site = siteView.getJSONObject("site");
JSONObject localSite = siteView.getJSONObject("local_site");
int id = site.getInt("id");
String name = site.getString("name");
String sidebar = null;
if (site.has("sidebar"))
sidebar = site.getString("sidebar");
String version = siteInfo.getString("version");
int id = site.getInt("id");
String name = site.getString("name");
String sidebar = null;
if (site.has("sidebar"))
sidebar = site.getString("sidebar");
String version = siteInfo.getString("version");
String description = null;
if (site.has("description"))
description = site.getString("description");
String description = null;
if (site.has("description"))
description = site.getString("description");
boolean enable_downvotes = localSite.getBoolean("enable_downvotes");
boolean enable_nsfw = localSite.getBoolean("enable_nsfw");
boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only");
boolean enable_downvotes = localSite.getBoolean("enable_downvotes");
boolean enable_nsfw = localSite.getBoolean("enable_nsfw");
boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only");
JSONObject counts = siteView.getJSONObject("counts");
List<BasicUserInfo> admins = new ArrayList<>();
if (siteInfo.has("admins")) {
JSONArray adminsJson = siteInfo.getJSONArray("admins");
for (int i = 0; i < adminsJson.length(); i++) {
JSONObject adminJson = adminsJson.getJSONObject(i).getJSONObject("person");
admins.add(new BasicUserInfo(adminJson.getInt("id"), adminJson.getString("name"),
LemmyUtils.actorID2FullName(adminJson.getString("actor_id")), adminJson.optString("avatar ", ""),
adminJson.optString("display_name", adminJson.getString("name")))
);
}
JSONObject counts = siteView.getJSONObject("counts");
List<BasicUserInfo> admins = new ArrayList<>();
if (siteInfo.has("admins")) {
JSONArray adminsJson = siteInfo.getJSONArray("admins");
for (int i = 0; i < adminsJson.length(); i++) {
JSONObject adminJson = adminsJson.getJSONObject(i).getJSONObject("person");
admins.add(new BasicUserInfo(adminJson.getInt("id"), adminJson.getString("name"),
LemmyUtils.actorID2FullName(adminJson.getString("actor_id")), adminJson.optString("avatar ", ""),
adminJson.optString("display_name", adminJson.getString("name")))
);
}
return new SiteInfo(id, name, version, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts));
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return new SiteInfo(id, name, version, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts));
}
}

View File

@ -0,0 +1,61 @@
package eu.toldi.infinityforlemmy.user;
import org.json.JSONException;
import org.json.JSONObject;
import eu.toldi.infinityforlemmy.utils.LemmyUtils;
public class MyUserInfo {
private final String qualifiedName;
private final String name;
private final String displayName;
private final String profileImageUrl;
private final String bannerImageUrl;
private MyUserInfo(String qualifiedName, String name, String displayName, String profileImageUrl, String bannerImageUrl) {
this.qualifiedName = qualifiedName;
this.name = name;
this.displayName = displayName;
this.profileImageUrl = profileImageUrl;
this.bannerImageUrl = bannerImageUrl;
}
public String getQualifiedName() {
return qualifiedName;
}
public String getName() {
return name;
}
public String getDisplayName() {
return displayName;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public String getBannerImageUrl() {
return bannerImageUrl;
}
public static MyUserInfo parseFromSiteInfo(String siteInfoJson) throws JSONException {
JSONObject siteInfo = new JSONObject(siteInfoJson);
JSONObject myUser = siteInfo.optJSONObject("my_user");
if (myUser == null) {
return null;
}
JSONObject localUserView = myUser.getJSONObject("local_user_view");
JSONObject person = localUserView.getJSONObject("person");
String qualifiedName = LemmyUtils.actorID2FullName(person.getString("actor_id"));
String name = person.getString("name");
String displayName = person.has("display_name") ? person.getString("display_name") : name;
String avatar = person.optString("avatar");
String banner = person.optString("banner");
return new MyUserInfo(qualifiedName, name, displayName, avatar, banner);
}
}

View File

@ -1265,7 +1265,7 @@
<string name="i_understand">I understand</string>
<string name="instance_url">Instance URL</string>
<string name="instance_url_hint">The URL of you preferred Lemmy instance with or without the https:// prefix</string>
<string name="user_username">Username</string>
<string name="user_username">Username or email</string>
<string name="user_password">Password</string>
<string name="user_2fa_token">2FA token (if needed)</string>
<string name="user_login">Login</string>