From a4d9e5eaf079d1ad480fe752bf15e76a1320fdc2 Mon Sep 17 00:00:00 2001 From: Alexei Stapinski Date: Tue, 29 Mar 2022 11:05:59 -0400 Subject: [PATCH] Implement Android's removeSessionCookies method --- README.md | 10 ++++++++++ .../cookies/CookieManagerModule.java | 14 ++++++++++++++ index.d.ts | 4 +++- index.js | 5 +++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 98584d9..15b0089 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,16 @@ CookieManager.flush() .then((success) => { console.log('CookieManager.flush =>', success); }); + +// Remove session cookies (ANDROID ONLY) +// Session cookies are cookies with no expires set. Android typically does not +// remove these, it is up to the developer to decide when to remove them. +// The return value is true if any session cookies were removed. +// iOS handles removal of session cookies automatically on app open. +CookieManager.removeSessionCookies() + .then((sessionCookiesRemoved) => { + console.log('CookieManager.removeSessionCookies =>', sessionCookiesRemoved); + }); ``` ### WebKit-Support (iOS only) diff --git a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java index 50c5f9d..abebe8a 100644 --- a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java +++ b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java @@ -102,6 +102,20 @@ public void flush(Promise promise) { } } + @ReactMethod + public void removeSessionCookies(Promise promise) { + try { + getCookieManager().removeSessionCookies(new ValueCallback() { + @Override + public void onReceiveValue(Boolean data) { + promise.resolve(data); + } + }); + } catch (Exception e) { + promise.reject(e); + } + } + @ReactMethod public void getFromResponse(String url, Promise promise) throws URISyntaxException, IOException { promise.resolve(url); diff --git a/index.d.ts b/index.d.ts index 133b18e..b2977de 100644 --- a/index.d.ts +++ b/index.d.ts @@ -23,9 +23,11 @@ declare module '@react-native-cookies/cookies' { clearAll(useWebKit?: boolean): Promise; + // Android only flush(): Promise; + removeSessionCookies(): Promise; - //iOS only + // iOS only getAll(useWebKit?: boolean): Promise; clearByName( url: string, diff --git a/index.js b/index.js index b83ae69..84c890a 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,11 @@ module.exports = { await CookieManager.flush(); } }, + removeSessionCookies: async () => { + if (Platform.OS === 'android') { + return await CookieManager.removeSessionCookies(); + } + }, }; for (var i = 0; i < functions.length; i++) {