Skip to content

Commit b459739

Browse files
authored
feat(Android): Implement Android's removeSessionCookies method (#145)
1 parent 167edee commit b459739

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ CookieManager.flush()
117117
.then((success) => {
118118
console.log('CookieManager.flush =>', success);
119119
});
120+
121+
// Remove session cookies (ANDROID ONLY)
122+
// Session cookies are cookies with no expires set. Android typically does not
123+
// remove these, it is up to the developer to decide when to remove them.
124+
// The return value is true if any session cookies were removed.
125+
// iOS handles removal of session cookies automatically on app open.
126+
CookieManager.removeSessionCookies()
127+
.then((sessionCookiesRemoved) => {
128+
console.log('CookieManager.removeSessionCookies =>', sessionCookiesRemoved);
129+
});
120130
```
121131

122132
### WebKit-Support (iOS only)

android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ public void flush(Promise promise) {
102102
}
103103
}
104104

105+
@ReactMethod
106+
public void removeSessionCookies(Promise promise) {
107+
try {
108+
getCookieManager().removeSessionCookies(new ValueCallback<Boolean>() {
109+
@Override
110+
public void onReceiveValue(Boolean data) {
111+
promise.resolve(data);
112+
}
113+
});
114+
} catch (Exception e) {
115+
promise.reject(e);
116+
}
117+
}
118+
105119
@ReactMethod
106120
public void getFromResponse(String url, Promise promise) throws URISyntaxException, IOException {
107121
promise.resolve(url);

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ declare module '@react-native-cookies/cookies' {
2323

2424
clearAll(useWebKit?: boolean): Promise<boolean>;
2525

26+
// Android only
2627
flush(): Promise<void>;
28+
removeSessionCookies(): Promise<boolean>;
2729

28-
//iOS only
30+
// iOS only
2931
getAll(useWebKit?: boolean): Promise<Cookies>;
3032
clearByName(
3133
url: string,

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ module.exports = {
4646
await CookieManager.flush();
4747
}
4848
},
49+
removeSessionCookies: async () => {
50+
if (Platform.OS === 'android') {
51+
return await CookieManager.removeSessionCookies();
52+
}
53+
},
4954
};
5055

5156
for (var i = 0; i < functions.length; i++) {

0 commit comments

Comments
 (0)