diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java index 741b861c..a575d468 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java @@ -67,6 +67,9 @@ public class LoginActivity extends BaseActivity { private static final String ENABLE_DOM_STATE = "EDS"; 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) CoordinatorLayout coordinatorLayout; @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); } + // 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() { @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java index 292bd562..76734fbb 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java @@ -248,6 +248,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private int fabOption; private int inboxCount; + private boolean mBearerTokenUsed = true; + @Override protected void onCreate(Bundle savedInstanceState) { SplashScreen.installSplashScreen(this); @@ -340,7 +342,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mRetrofit.setAccessToken(mAccessToken); - + mBearerTokenUsed = mCurrentAccountSharedPreferences.getBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); @@ -374,6 +376,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb mRetrofit.setBaseURL(instancePreference); 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() { if (!mFetchUserInfoSuccess) { FetchUserData.fetchUnreadCount(mRetrofit.getRetrofit(), mAccessToken, new FetchUserData.FetchUserUnreadCountListener() { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java index 8604524f..21313740 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java @@ -66,6 +66,9 @@ public interface LemmyAPI { @GET("api/v3/user/unread_count") Call userUnreadCount(@NonNull @Query("auth") String access_token); + @GET("api/v3/user/validate_auth") + Call userValidateAuth(); + @Headers("Content-Type: application/json") @POST("api/v3/user/mention/mark_as_read") Call userMentionMarkAsRead(@Body ReadMessageDTO params); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/user/FetchUserData.java b/app/src/main/java/eu/toldi/infinityforlemmy/user/FetchUserData.java index fed41d94..5f27d706 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/user/FetchUserData.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/user/FetchUserData.java @@ -107,6 +107,33 @@ public class FetchUserData { }); } + public static void validateAuthToken(Retrofit retrofit, ValidateAuthTokenListener validateAuthTokenListener) { + LemmyAPI api = retrofit.create(LemmyAPI.class); + + Call validateAuthToken = api.userValidateAuth(); + validateAuthToken.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + if (response.isSuccessful()) { + validateAuthTokenListener.onValidateAuthTokenSuccess(); + } else { + validateAuthTokenListener.onValidateAuthTokenFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + validateAuthTokenListener.onValidateAuthTokenFailed(); + } + }); + } + + public interface ValidateAuthTokenListener { + void onValidateAuthTokenSuccess(); + + void onValidateAuthTokenFailed(); + } + public interface FetchUserUnreadCountListener { void onFetchUserUnreadCountSuccess(int unreadCount); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5fd0c77a..fbe38438 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1322,4 +1322,6 @@ "Infinity was created by u/Hostilenemy " The URL of you prefered Lemmy instance with or without the https:// prefix Scaled + Token Expired + "Your token has been expired. As Eternity does not store your password, you need to manually log back in! " \ No newline at end of file