Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

72: Added useWebKit support for getFromResponse and setFromResponse. #105

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Cookie Manager for React Native
This module was ported from [joeferraro/react-native-cookies](https://github.com/joeferraro/react-native-cookies). This would not exist without the work of the original author, [Joe Ferraro](https://github.com/joeferraro).

## Important notices & Breaking Changes

- **v6.0.0**: Package name updated to `@react-native-cookies/cookies`.
- **v5.0.0**: Peer Dependency of >= React Native 0.60.2
- **v4.0.0**: Android SDK version bumpted to 21
Expand Down Expand Up @@ -131,13 +132,15 @@ To use WebKit-Support, you should be able to simply make advantage of the react-

To use this _CookieManager_ with WebKit-Support we extended the interface with the attribute `useWebKit` (a boolean value, default: `FALSE`) for the following methods:

| Method | WebKit-Support | Method-Signature |
| ----------- | -------------- | ------------------------------------------------------------------------ |
| getAll | Yes | `CookieManager.getAll(useWebKit:boolean)` |
| clearAll | Yes | `CookieManager.clearAll(useWebKit:boolean)` |
| clearByName | Yes | `CookieManager.clearByName(url:string, name: string, useWebKit:boolean)` |
| get | Yes | `CookieManager.get(url:string, useWebKit:boolean)` |
| set | Yes | `CookieManager.set(url:string, cookie:object, useWebKit:boolean)` |
| Method | WebKit-Support | Method-Signature |
| --------------- | -------------- | ----------------------------------------------------------------------------- |
| getAll | Yes | `CookieManager.getAll(useWebKit:boolean)` |
| clearAll | Yes | `CookieManager.clearAll(useWebKit:boolean)` |
| clearByName | Yes | `CookieManager.clearByName(url:string, name: string, useWebKit:boolean)` |
| get | Yes | `CookieManager.get(url:string, useWebKit:boolean)` |
| getFromResponse | Yes | `CookieManager.getFromResponse(url:string, useWebKit:boolean)` |
| set | Yes | `CookieManager.set(url:string, cookie:object, useWebKit:boolean)` |
| setFromResponse | Yes | `CookieManager.setFromResponse(url:string, cookie:object, useWebKit:boolean)` |

##### Usage

Expand Down Expand Up @@ -170,6 +173,12 @@ CookieManager.get('http://example.com', useWebKit)
console.log('CookieManager.get from webkit-view =>', cookies);
});

// Get cookies as a request header string
CookieManager.getFromResponse('http://example.com', useWebKit)
.then((cookies) => {
console.log('CookieManager.getFromResponse from webkit-view =>', cookies);
});

// set a cookie
const newCookie: = {
name: 'myCookie',
Expand All @@ -180,8 +189,21 @@ const newCookie: = {
expires: '2015-05-30T12:30:00.00-05:00'
};

// set a cookie
CookieManager.set('http://example.com', newCookie, useWebKit)
.then((res) => {
console.log('CookieManager.set from webkit-view =>', res);
});

// Set cookies from a response header
// This allows you to put the full string provided by a server's Set-Cookie
// response header directly into the cookie store.
CookieManager.setFromResponse(
'http://example.com',
'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly',
useWebKit)
.then((success) => {
console.log('CookieManager.setFromResponse from webkit-view =>', success);
});

```
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void set(String url, ReadableMap cookie, Boolean useWebKit, final Promise
}

@ReactMethod
public void setFromResponse(String url, String cookie, final Promise promise) {
public void setFromResponse(String url, String cookie, Boolean useWebKit, final Promise promise) {
if (cookie == null) {
promise.reject(new Exception(INVALID_COOKIE_VALUES));
return;
Expand All @@ -96,7 +96,7 @@ public void flush(Promise promise) {
}

@ReactMethod
public void getFromResponse(String url, Promise promise) throws URISyntaxException, IOException {
public void getFromResponse(String url, Boolean useWebKit, Promise promise) throws URISyntaxException, IOException {
promise.resolve(url);
}

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ declare module '@react-native-cookies/cookies' {

export interface CookieManagerStatic {
set(url: string, cookie: Cookie, useWebKit?: boolean): Promise<boolean>;
setFromResponse(url: string, cookie: string): Promise<boolean>;
setFromResponse(url: string, cookie: string, useWebKit?: boolean): Promise<boolean>;

get(url: string, useWebKit?: boolean): Promise<Cookies>;
getFromResponse(url: string): Promise<Cookies>;
getFromResponse(url: string, useWebKit?: boolean): Promise<Cookies>;

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

Expand Down
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ if (Platform.OS === 'ios') {
);
}

const functions = ['setFromResponse', 'getFromResponse'];

module.exports = {
getAll: (useWebKit = false) => CookieManager.getAll(useWebKit),
clearAll: (useWebKit = false) => CookieManager.clearAll(useWebKit),
get: (url, useWebKit = false) => CookieManager.get(url, useWebKit),
getFromResponse: (url, useWebKit = false) =>
CookieManager.getFromResponse(url, useWebKit),
set: (url, cookie, useWebKit = false) =>
CookieManager.set(url, cookie, useWebKit),
setFromResponse: (url, cookie, useWebKit = false) =>
CookieManager.setFromResponse(url, cookie, useWebKit),
clearByName: (url, name, useWebKit = false) =>
CookieManager.clearByName(url, name, useWebKit),
flush: async () => {
Expand All @@ -47,7 +49,3 @@ module.exports = {
}
},
};

for (var i = 0; i < functions.length; i++) {
module.exports[functions[i]] = CookieManager[functions[i]];
}
54 changes: 44 additions & 10 deletions ios/RNCookieManagerIOS/RNCookieManagerIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,33 @@ + (BOOL)requiresMainQueueSetup
RCT_EXPORT_METHOD(
setFromResponse:(NSURL *)url
cookie:(NSString *)cookie
useWebKit:(BOOL)useWebKit
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:@{@"Set-Cookie": cookie} forURL:url];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil];
resolve(@(YES));
if (useWebKit) {
if (@available(iOS 11.0, *)) {
dispatch_async(dispatch_get_main_queue(), ^(){
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
for (NSHTTPCookie* cookie in cookies)
{
[cookieStore setCookie:cookie completionHandler:nil];
}
resolve(@(YES));
});
} else {
reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil);
}
} else {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil];
resolve(@(YES));
}
}

RCT_EXPORT_METHOD(
getFromResponse:(NSURL *)url
useWebKit:(BOOL)useWebKit
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Expand All @@ -96,14 +113,31 @@ + (BOOL)requiresMainQueueSetup

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:response.URL];
NSMutableDictionary *dics = [NSMutableDictionary dictionary];

for (int i = 0; i < cookies.count; i++) {
NSHTTPCookie *cookie = [cookies objectAtIndex:i];
[dics setObject:cookie.value forKey:cookie.name];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
resolve(dics);

if (useWebKit) {
if (@available(iOS 11.0, *)) {
dispatch_async(dispatch_get_main_queue(), ^(){
NSMutableDictionary *dics = [NSMutableDictionary dictionary];
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
for (int i = 0; i < cookies.count; i++) {
NSHTTPCookie *cookie = [cookies objectAtIndex:i];
[dics setObject:cookie.value forKey:cookie.name];
[cookieStore setCookie:cookie completionHandler:nil];
}
resolve(dics);
});
} else {
reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil);
}
} else {
NSMutableDictionary *dics = [NSMutableDictionary dictionary];
for (int i = 0; i < cookies.count; i++) {
NSHTTPCookie *cookie = [cookies objectAtIndex:i];
[dics setObject:cookie.value forKey:cookie.name];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
resolve(dics);
}
}];
}

Expand Down