Check the user token when opening the app

This commit is contained in:
Balazs Toldi 2024-01-04 11:03:29 +01:00
parent 40a9091b7d
commit 8a9102bc8a
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
5 changed files with 79 additions and 1 deletions

View File

@ -67,6 +67,9 @@ public class LoginActivity extends BaseActivity {
private static final String ENABLE_DOM_STATE = "EDS"; private static final String ENABLE_DOM_STATE = "EDS";
private static final String IS_AGREE_TO_USER_AGGREMENT_STATE = "IATUAS"; private static final String IS_AGREE_TO_USER_AGGREMENT_STATE = "IATUAS";
public static final String EXTRA_INPUT_USERNAME = "INPUT_USERNAME";
public static final String EXTRA_INPUT_INSTANCE = "INPUT_INSTANCE";
@BindView(R.id.coordinator_layout_login_activity) @BindView(R.id.coordinator_layout_login_activity)
CoordinatorLayout coordinatorLayout; CoordinatorLayout coordinatorLayout;
@BindView(R.id.appbar_layout_login_activity) @BindView(R.id.appbar_layout_login_activity)
@ -149,6 +152,17 @@ public class LoginActivity extends BaseActivity {
isAgreeToUserAgreement = savedInstanceState.getBoolean(IS_AGREE_TO_USER_AGGREMENT_STATE); isAgreeToUserAgreement = savedInstanceState.getBoolean(IS_AGREE_TO_USER_AGGREMENT_STATE);
} }
// Get username and instance from intent
Intent intent = getIntent();
String username_intent = intent.getStringExtra(EXTRA_INPUT_USERNAME);
String instance_intent = intent.getStringExtra(EXTRA_INPUT_INSTANCE);
if (username_intent != null) {
username_input.setText(username_intent);
}
if (instance_intent != null) {
instance_input.setText(instance_intent);
}
LemmyVerseFetchInstances.INSTANCE.fetchInstances(mLemmyVerseRetrofit, new FetchInstancesListener() { LemmyVerseFetchInstances.INSTANCE.fetchInstances(mLemmyVerseRetrofit, new FetchInstancesListener() {
@Override @Override

View File

@ -248,6 +248,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private int fabOption; private int fabOption;
private int inboxCount; private int inboxCount;
private boolean mBearerTokenUsed = true;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this); SplashScreen.installSplashScreen(this);
@ -340,7 +342,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mRetrofit.setAccessToken(mAccessToken); mRetrofit.setAccessToken(mAccessToken);
mBearerTokenUsed = mCurrentAccountSharedPreferences.getBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true);
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null);
@ -374,6 +376,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
mRetrofit.setBaseURL(instancePreference); mRetrofit.setBaseURL(instancePreference);
this.recreate(); this.recreate();
} }
} else {
checkUserToken();
} }
} }
@ -1081,6 +1085,34 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
private void checkUserToken() {
if (mBearerTokenUsed) {
FetchUserData.validateAuthToken(mRetrofit.getRetrofit(), new FetchUserData.ValidateAuthTokenListener() {
@Override
public void onValidateAuthTokenSuccess() {
}
@Override
public void onValidateAuthTokenFailed() {
// Alert user that the token is invalid and they need to re-login
new MaterialAlertDialogBuilder(MainActivity.this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.token_expired)
.setMessage(R.string.token_expired_message)
.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
// Username without instance
String username = mAccountName.substring(0, mAccountQualifiedName.indexOf("@"));
intent.putExtra(LoginActivity.EXTRA_INPUT_USERNAME, username);
intent.putExtra(LoginActivity.EXTRA_INPUT_INSTANCE, mRetrofit.getBaseURL());
startActivity(intent);
})
.setCancelable(false)
.show();
}
});
}
}
private void loadUserData() { private void loadUserData() {
if (!mFetchUserInfoSuccess) { if (!mFetchUserInfoSuccess) {
FetchUserData.fetchUnreadCount(mRetrofit.getRetrofit(), mAccessToken, new FetchUserData.FetchUserUnreadCountListener() { FetchUserData.fetchUnreadCount(mRetrofit.getRetrofit(), mAccessToken, new FetchUserData.FetchUserUnreadCountListener() {

View File

@ -66,6 +66,9 @@ public interface LemmyAPI {
@GET("api/v3/user/unread_count") @GET("api/v3/user/unread_count")
Call<MessageCount> userUnreadCount(@NonNull @Query("auth") String access_token); Call<MessageCount> userUnreadCount(@NonNull @Query("auth") String access_token);
@GET("api/v3/user/validate_auth")
Call<String> userValidateAuth();
@Headers("Content-Type: application/json") @Headers("Content-Type: application/json")
@POST("api/v3/user/mention/mark_as_read") @POST("api/v3/user/mention/mark_as_read")
Call<String> userMentionMarkAsRead(@Body ReadMessageDTO params); Call<String> userMentionMarkAsRead(@Body ReadMessageDTO params);

View File

@ -107,6 +107,33 @@ public class FetchUserData {
}); });
} }
public static void validateAuthToken(Retrofit retrofit, ValidateAuthTokenListener validateAuthTokenListener) {
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<String> validateAuthToken = api.userValidateAuth();
validateAuthToken.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
validateAuthTokenListener.onValidateAuthTokenSuccess();
} else {
validateAuthTokenListener.onValidateAuthTokenFailed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
validateAuthTokenListener.onValidateAuthTokenFailed();
}
});
}
public interface ValidateAuthTokenListener {
void onValidateAuthTokenSuccess();
void onValidateAuthTokenFailed();
}
public interface FetchUserUnreadCountListener { public interface FetchUserUnreadCountListener {
void onFetchUserUnreadCountSuccess(int unreadCount); void onFetchUserUnreadCountSuccess(int unreadCount);

View File

@ -1322,4 +1322,6 @@
<string name="settings_credits_original_app_description">"Infinity was created by u/Hostilenemy "</string> <string name="settings_credits_original_app_description">"Infinity was created by u/Hostilenemy "</string>
<string name="the_url_of_you_prefered_lemmy_instance_with_or_without_the_https_prefix">The URL of you prefered Lemmy instance with or without the https:// prefix</string> <string name="the_url_of_you_prefered_lemmy_instance_with_or_without_the_https_prefix">The URL of you prefered Lemmy instance with or without the https:// prefix</string>
<string name="sort_scaled">Scaled</string> <string name="sort_scaled">Scaled</string>
<string name="token_expired">Token Expired</string>
<string name="token_expired_message">"Your token has been expired. As Eternity does not store your password, you need to manually log back in! "</string>
</resources> </resources>