Skip to content

Commit 33610d7

Browse files
author
maskara
committed
Fix logout
1 parent b584942 commit 33610d7

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.content.SharedPreferences;
56
import android.database.sqlite.SQLiteDatabase;
@@ -128,6 +129,7 @@ public static RefWatcher getRefWatcher(Context context) {
128129
* @param context Application context
129130
* @param logoutListener Implementation of interface LogoutListener
130131
*/
132+
@SuppressLint("CheckResult")
131133
public void clearApplicationData(Context context, LogoutListener logoutListener) {
132134
File cacheDirectory = context.getCacheDir();
133135
File applicationDirectory = new File(cacheDirectory.getParent());
@@ -140,7 +142,7 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
140142
}
141143
}
142144

143-
sessionManager.clearAllAccounts()
145+
sessionManager.logout()
144146
.subscribeOn(Schedulers.io())
145147
.observeOn(AndroidSchedulers.mainThread())
146148
.subscribe(() -> {
@@ -151,7 +153,6 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
151153
applicationPrefs.edit().putBoolean("firstrun", false).apply();
152154
otherPrefs.edit().clear().apply();
153155
updateAllDatabases();
154-
155156
logoutListener.onLogoutComplete();
156157
});
157158
}

app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,19 @@ public void forceLogin(Context context) {
153153
}
154154
}
155155

156-
public Completable clearAllAccounts() {
156+
/**
157+
* 1. Clears existing accounts from account manager
158+
* 2. Calls MediaWikiApi's logout function to clear cookies
159+
* @return
160+
*/
161+
public Completable logout() {
157162
AccountManager accountManager = AccountManager.get(context);
158163
Account[] allAccounts = accountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE);
159164
return Completable.fromObservable(Observable.fromArray(allAccounts)
160165
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
161-
.doOnComplete(() -> currentAccount = null);
166+
.doOnComplete(() -> {
167+
mediaWikiApi.logout();
168+
currentAccount = null;
169+
});
162170
}
163171
}

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,15 @@ private Date parseMWDate(String mwDate) {
10321032
}
10331033
}
10341034

1035+
/**
1036+
* Calls media wiki's logout API
1037+
*/
1038+
public void logout() {
1039+
try {
1040+
api.logout();
1041+
} catch (IOException e) {
1042+
Timber.e(e, "Error occurred while logging out");
1043+
}
1044+
}
1045+
10351046
}

app/src/main/java/fr/free/nrw/commons/mwapi/CustomMwApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public void logout() throws IOException {
167167
// I should be doing more validation here, but meh
168168
isLoggedIn = false;
169169
this.action("logout").post();
170+
removeAllCookies();
171+
authCookie = null;
170172
}
171173

172174
private CustomApiResult makeRequest(String method, HashMap<String, Object> params) throws IOException {

app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public interface MediaWikiApi {
106106
@NonNull
107107
Single<JSONObject> getAchievements(String userName);
108108

109+
void logout();
110+
109111
interface ProgressListener {
110112
void onProgress(long transferred, long total);
111113
}

0 commit comments

Comments
 (0)