Skip to content

Commit 5410f6e

Browse files
committed
feat: clearCookies method
1 parent d431e88 commit 5410f6e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/request.android.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export function disableSSLPinning() {
229229
peer.enabled = false;
230230
getClient(undefined, true);
231231
}
232+
const SDKVersion = android.os.Build.VERSION.SDK_INT;
232233

233234
let Client: okhttp3.OkHttpClient;
234235
let cookieJar: com.nativescript.https.QuotePreservingCookieJar;
@@ -237,8 +238,7 @@ export function getClient(opts: Partial<HttpsRequestOptions> = {}, reload: boole
237238
if (!Client) {
238239
// ssl error fix on KitKat. Only need to be done once.
239240
// client will be null only onced so will run only once
240-
const version = android.os.Build.VERSION.SDK_INT;
241-
if (version >= 16 && version < 22 && (org as any).conscrypt) {
241+
if (SDKVersion >= 16 && SDKVersion < 22 && (org as any).conscrypt) {
242242
java.security.Security.insertProviderAt((org as any).conscrypt.Conscrypt.newProvider(), 1);
243243
}
244244
}
@@ -401,6 +401,13 @@ export function cancelRequest(tag: string, client: okhttp3.OkHttpClient = runnin
401401
}
402402
}
403403

404+
export function clearCookies() {
405+
if (cookieJar) {
406+
cookieJar = null;
407+
cookieManager = null;
408+
}
409+
}
410+
404411
let CALL_ID = 0;
405412
const notClosedResponses: {
406413
[k: string]: com.nativescript.https.OkHttpResponse;

src/request.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface HttpsRequestOptions extends HttpRequestOptions {
5959
cachePolicy?: CachePolicy;
6060

6161
/**
62-
* default to true
62+
* default to true. Android and iOS only store cookies in memory! it will be cleared after an app restart
6363
*/
6464
cookiesEnabled?: boolean;
6565
}
@@ -109,6 +109,7 @@ export function setCache(options?: CacheOptions);
109109
export function clearCache();
110110
export function createRequest(opts: HttpsRequestOptions): HttpsRequest;
111111
export function cancelRequest(tag: string);
112+
export function clearCookies();
112113
export function addNetworkInterceptor(interceptor);
113114

114115
export function getClient(opts: Partial<HttpsRequestOptions>);

src/request.ios.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ export function cancelRequest(tag: string) {
309309
}
310310
}
311311

312+
export function clearCookies() {
313+
const storage = NSHTTPCookieStorage.sharedHTTPCookieStorage;
314+
const cookies = storage.cookies;
315+
cookies.enumerateObjectsUsingBlock((cookie) => {
316+
storage.deleteCookie(cookie);
317+
});
318+
}
312319
export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = true): HttpsRequest {
313320
const type = opts.headers && opts.headers['Content-Type'] ? opts.headers['Content-Type'] : 'application/json';
314321
if (type.startsWith('application/json')) {
@@ -361,8 +368,8 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
361368

362369
const progress = opts.onProgress
363370
? (progress: NSProgress) => {
364-
opts.onProgress(progress.completedUnitCount, progress.totalUnitCount);
365-
}
371+
opts.onProgress(progress.completedUnitCount, progress.totalUnitCount);
372+
}
366373
: null;
367374
let task: NSURLSessionDataTask;
368375
const tag = opts.tag;

0 commit comments

Comments
 (0)