Skip to content
This repository was archived by the owner on Jan 24, 2020. It is now read-only.

Commit fcbc22f

Browse files
committed
Merge branch 'Psykar-master'
2 parents cbfbfa2 + b8f8bbd commit fcbc22f

File tree

8 files changed

+210
-37
lines changed

8 files changed

+210
-37
lines changed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ Cookie manager for react native.
44

55
### Installation
66

7+
#### IOS
78
1. `npm install react-native-cookies`
89
2. In the XCode's "Project navigator", right click on project name folder ➜ `Add Files to <...>`
910
- Ensure `Copy items if needed` and `Create groups` are checked
1011
3. Go to `node_modules``react-native-cookies` ➜ add `RNCookieManagerIOS` folder
1112
4. Compile and have some cookies
1213

14+
#### android
15+
1. `npm install react-native-cookies`
16+
2. `rnpm link react-native-cookies` - (run `npm install -g rnpm` if required)
17+
3. Om nom nom nom cookies.
18+
1319
### Usage
1420

1521
```javascript
1622
var CookieManager = require('react-native-cookies');
1723

18-
// set a cookie
24+
// set a cookie (IOS ONLY)
1925
CookieManager.set({
2026
name: 'myCookie',
2127
value: 'myValue',
@@ -30,7 +36,21 @@ CookieManager.set({
3036
console.log(res);
3137
});
3238

33-
// list cookies
39+
// Set cookies from a response header
40+
// This allows you to put the full string provided by a server's Set-Cookie
41+
// response header directly into the cookie store.
42+
CookieManager.setFromResponse('http://example.com', 'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly', (res) => {
43+
// `res` will be true or false depending on success.
44+
console.log("Set cookie", res);
45+
})
46+
47+
// Get cookies as a request header string
48+
CookieManager.get('http://example.com', (res) => {
49+
console.log('Got cookies for url', res);
50+
// Outputs 'user_session=abcdefg; path=/;'
51+
})
52+
53+
// list cookies (IOS ONLY)
3454
CookieManager.getAll((err, res) => {
3555
console.log('cookies!');
3656
console.log(err);
@@ -48,7 +68,6 @@ CookieManager.clearAll((err, res) => {
4868

4969
### Roadmap
5070

51-
- Expose `get` method
5271
- Proper `getAll` dictionary by domain
5372
- Proper error handling
5473
- Anything else?

RNCookieManagerIOS/RNCookieManagerIOS.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@ @implementation RNCookieManagerIOS
3232
callback(@[[NSNull null], @"success"]);
3333
}
3434

35-
// TODO: implement
36-
// RCT_EXPORT_METHOD(get:(NSString *)name) {
37-
// callback(@[[NSNull null], @"success"]);
38-
// }
35+
RCT_EXPORT_METHOD(setFromResponse:(NSURL *)url value:(NSDictionary *)value callback:(RCTResponseSenderBlock)callback) {
36+
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:value forURL:url];
37+
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:NULL];
38+
callback(@[[NSNull null], @"success"]);
39+
}
40+
41+
42+
RCT_EXPORT_METHOD(get:(NSURL *)url callback:(RCTResponseSenderBlock)callback) {
43+
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
44+
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
45+
if ([headers objectForKey:@"Cookie"] == nil) {
46+
callback(@[[NSNull null], @"success"]);
47+
} else {
48+
callback(@[headers[@"Cookie"], @"success"]);
49+
}
50+
}
3951

4052
RCT_EXPORT_METHOD(clearAll:(RCTResponseSenderBlock)callback) {
4153
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

android/build.gradle

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
6+
dependencies {
7+
classpath 'com.android.tools.build:gradle:1.1.3'
8+
}
9+
}
10+
11+
apply plugin: 'com.android.library'
12+
13+
android {
14+
compileSdkVersion 23
15+
buildToolsVersion "23.0.1"
16+
17+
defaultConfig {
18+
minSdkVersion 16
19+
targetSdkVersion 22
20+
versionCode 1
21+
versionName "1.0"
22+
}
23+
lintOptions {
24+
abortOnError false
25+
}
26+
}
27+
28+
repositories {
29+
mavenCentral()
30+
}
31+
32+
dependencies {
33+
compile 'com.facebook.react:react-native:0.16.+'
34+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.psykar.cookiemanager" >
4+
</manifest>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.psykar.cookiemanager;
2+
3+
import com.facebook.react.modules.network.ForwardingCookieHandler;
4+
import com.facebook.react.bridge.ReactApplicationContext;
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
6+
import com.facebook.react.bridge.ReactMethod;
7+
import com.facebook.react.bridge.ReadableMap;
8+
import com.facebook.react.bridge.Callback;
9+
10+
import java.io.IOException;
11+
import java.net.URISyntaxException;
12+
import java.net.URI;
13+
import java.util.Collections;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
19+
public class CookieManagerModule extends ReactContextBaseJavaModule {
20+
21+
private ForwardingCookieHandler cookieHandler;
22+
23+
public CookieManagerModule(ReactApplicationContext context) {
24+
super(context);
25+
this.cookieHandler = new ForwardingCookieHandler(context);
26+
}
27+
28+
public String getName() {
29+
return "RNCookieManagerAndroid";
30+
}
31+
32+
@ReactMethod
33+
public void set(ReadableMap cookie, final Callback callback) throws Exception {
34+
throw new Exception("Cannot call on android, try setFromResponse");
35+
}
36+
37+
@ReactMethod
38+
public void setFromResponse(String url, String value, final Callback callback) throws URISyntaxException, IOException {
39+
Map headers = new HashMap<String, List<String>>();
40+
// Pretend this is a header
41+
headers.put("Set-cookie", Collections.singletonList(value));
42+
URI uri = new URI(url);
43+
this.cookieHandler.put(uri, headers);
44+
callback.invoke(true);
45+
}
46+
47+
@ReactMethod
48+
public void getAll(Callback callback) throws Exception {
49+
throw new Exception("Cannot get all cookies on android, try getCookieHeader(url)");
50+
}
51+
52+
@ReactMethod
53+
public void get(String url, Callback callback) throws URISyntaxException, IOException {
54+
URI uri = new URI(url);
55+
56+
Map<String, List<String>> cookieMap = this.cookieHandler.get(uri, new HashMap());
57+
// If only the variables were public
58+
List<String> cookieList = cookieMap.get("Cookie");
59+
if (cookieList != null) {
60+
callback.invoke(cookieList.get(0));
61+
} else {
62+
callback.invoke(cookieList);
63+
}
64+
}
65+
66+
@ReactMethod
67+
public void clearAll(final Callback callback) {
68+
this.cookieHandler.clearCookies(callback);
69+
}
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.psykar.cookiemanager;
2+
3+
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.NativeModule;
5+
import com.facebook.react.bridge.JavaScriptModule;
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.facebook.react.uimanager.ViewManager;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
public class CookieManagerPackage implements ReactPackage {
15+
16+
public CookieManagerPackage() {}
17+
18+
@Override
19+
public List<NativeModule> createNativeModules(
20+
ReactApplicationContext reactContext) {
21+
List<NativeModule> modules = new ArrayList<>();
22+
23+
modules.add(new CookieManagerModule(reactContext));
24+
return modules;
25+
}
26+
27+
@Override
28+
public List<Class<? extends JavaScriptModule>> createJSModules() {
29+
return Collections.emptyList();
30+
}
31+
32+
@Override
33+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
34+
return Arrays.<ViewManager>asList();
35+
}
36+
}

index.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
var NativeModules = require('react-native').NativeModules;
1+
var React = require('react-native');
2+
var NativeModules = React.NativeModules;
3+
var Platform = React.Platform;
24
var invariant = require('invariant');
35
var RNCookieManagerIOS = NativeModules.RNCookieManagerIOS;
6+
var RNCookieManagerAndroid = NativeModules.RNCookieManagerAndroid;
47

5-
module.exports = {
6-
set(props, callback) {
7-
invariant(
8-
NativeModules.RNCookieManagerIOS,
9-
'Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project.'
10-
);
11-
RNCookieManagerIOS.set(props, (err, res) => {
12-
callback(err, res);
13-
});
14-
},
15-
clearAll(callback) {
16-
invariant(
17-
NativeModules.RNCookieManagerIOS,
18-
'Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project.'
19-
);
20-
RNCookieManagerIOS.clearAll((err, res) => {
21-
callback(err, res);
22-
});
23-
},
24-
getAll(callback) {
25-
invariant(
26-
NativeModules.RNCookieManagerIOS,
27-
'Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project.'
28-
);
29-
RNCookieManagerIOS.getAll((err, res) => {
30-
callback(err, res);
31-
});
32-
}
8+
var CookieManager;
9+
if (Platform.OS === 'ios') {
10+
invariant(RNCookieManagerIOS,
11+
'Add RNCookieMangerIOS.h and RNCookieManagerIOS.m to your Xcode project');
12+
CookieManager = RNCookieManagerIOS;
13+
} else if (Platform.OS === 'android') {
14+
invariant(RNCookieManagerAndroid, 'Import libraries to android "rnpm link"');
15+
CookieManager = RNCookieManagerAndroid;
16+
} else {
17+
invariant(CookieManager, "Invalid platform");
3318
}
19+
20+
var functions = [
21+
'set',
22+
'setFromResponse',
23+
'get',
24+
'getAll',
25+
'clearAll'
26+
];
27+
28+
module.exports = {}
29+
for (var i=0; i < functions.length; i++) {
30+
module.exports[functions[i]] = CookieManager[functions[i]];
31+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-cookies",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Cookie manager for react native",
55
"main": "index.js",
66
"author": "@joeferraro",

0 commit comments

Comments
 (0)