Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributions loading fix #1433

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected void requestAuthToken() {
authCookie = sessionManager.getAuthCookie();
if (authCookie != null) {
onAuthCookieAcquired(authCookie);
} else {
sessionManager.forceLogin(AuthenticatedActivity.this);
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
Expand Down Expand Up @@ -405,6 +406,11 @@ private void showMessage(@StringRes int resId, @ColorRes int colorResId) {
errorMessageContainer.setVisibility(VISIBLE);
}

public static void startYourself(Context context) {
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);
}

private AppCompatDelegate getDelegate() {
if (delegate == null) {
delegate = AppCompatDelegate.create(this, null);
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import fr.free.nrw.commons.mwapi.MediaWikiApi;
import io.reactivex.Completable;
Expand Down Expand Up @@ -60,21 +61,40 @@ public Boolean revalidateAuthToken() {
return true;
}

/**
* Anyone calling this function should check if the auth cookie was returned and accordingly handle exceptional scenario.
* @return
*/
@Nullable
public String getAuthCookie() {
boolean isLoggedIn = sharedPreferences.getBoolean("isUserLoggedIn", false);

if (!isLoggedIn) {
Timber.e("User is not logged in");
return null;
} else {
String authCookie = sharedPreferences.getString("getAuthCookie", null);
String authCookie = getGetAuthCookieFromStorage();
if (authCookie == null) {
Timber.e("Auth cookie is null even after login");
if (revalidateAuthToken()) {
return getGetAuthCookieFromStorage();
} else {
Timber.e("Auth cookie is null even after login");
}
}
return authCookie;
}
}

private String getGetAuthCookieFromStorage() {
return sharedPreferences.getString("getAuthCookie", null);
}

public void forceLogin(Context context) {
if (context != null) {
LoginActivity.startYourself(context);
}
}

public Completable clearAllAccounts() {
AccountManager accountManager = AccountManager.get(context);
Account[] allAccounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void onPerformSync(Account account, Bundle bundle, String s, ContentProvi

String authCookie = sessionManager.getAuthCookie();
if (isNullOrWhiteSpace(authCookie)) {
sessionManager.forceLogin(getContext());
Timber.d("Could not authenticate :(");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,14 @@ private String getErrorCodeToReturn(ApiResult loginApiResult) {
}

private void setAuthCookieOnLogin(boolean isLoggedIn) {
saveAuthCookie(isLoggedIn, api.getAuthCookie());
}

private void saveAuthCookie(boolean isLoggedIn, String authCookie) {
SharedPreferences.Editor editor = sharedPreferences.edit();
if (isLoggedIn) {
editor.putBoolean("isUserLoggedIn", true);
editor.putString("getAuthCookie", api.getAuthCookie());
editor.putString("getAuthCookie", authCookie);
} else {
editor.putBoolean("isUserLoggedIn", false);
editor.remove("getAuthCookie");
Expand All @@ -179,6 +183,7 @@ public String getAuthCookie() {
@Override
public void setAuthCookie(String authCookie) {
api.setAuthCookie(authCookie);
saveAuthCookie(true, authCookie);
}

@Override
Expand Down