Fix logout (#1875)

This commit is contained in:
Vivek Maskara 2018-09-05 13:55:09 +05:30 committed by Josephine Lim
parent b5849420fc
commit 54caad265a
5 changed files with 28 additions and 4 deletions

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
@ -128,6 +129,7 @@ public class CommonsApplication extends MultiDexApplication {
* @param context Application context
* @param logoutListener Implementation of interface LogoutListener
*/
@SuppressLint("CheckResult")
public void clearApplicationData(Context context, LogoutListener logoutListener) {
File cacheDirectory = context.getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
@ -140,7 +142,7 @@ public class CommonsApplication extends MultiDexApplication {
}
}
sessionManager.clearAllAccounts()
sessionManager.logout()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
@ -151,7 +153,6 @@ public class CommonsApplication extends MultiDexApplication {
applicationPrefs.edit().putBoolean("firstrun", false).apply();
otherPrefs.edit().clear().apply();
updateAllDatabases();
logoutListener.onLogoutComplete();
});
}

View file

@ -153,11 +153,19 @@ public class SessionManager {
}
}
public Completable clearAllAccounts() {
/**
* 1. Clears existing accounts from account manager
* 2. Calls MediaWikiApi's logout function to clear cookies
* @return
*/
public Completable logout() {
AccountManager accountManager = AccountManager.get(context);
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
return Completable.fromObservable(Observable.fromArray(allAccounts)
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
.doOnComplete(() -> currentAccount = null);
.doOnComplete(() -> {
mediaWikiApi.logout();
currentAccount = null;
});
}
}

View file

@ -1032,4 +1032,15 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
}
}
/**
* Calls media wiki's logout API
*/
public void logout() {
try {
api.logout();
} catch (IOException e) {
Timber.e(e, "Error occurred while logging out");
}
}
}

View file

@ -167,6 +167,8 @@ public class CustomMwApi {
// I should be doing more validation here, but meh
isLoggedIn = false;
this.action("logout").post();
removeAllCookies();
authCookie = null;
}
private CustomApiResult makeRequest(String method, HashMap<String, Object> params) throws IOException {

View file

@ -106,6 +106,8 @@ public interface MediaWikiApi {
@NonNull
Single<JSONObject> getAchievements(String userName);
void logout();
interface ProgressListener {
void onProgress(long transferred, long total);
}