Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface Cookie {
expires?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: string;
}

export interface Cookies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public String getName() {
public void set(String url, ReadableMap cookie, Boolean useWebKit, final Promise promise) {
String cookieString = null;
try {
cookieString = toRFC6265string(makeHTTPCookieObject(url, cookie));
String sameSite = cookie.getString("sameSite");
cookieString = toRFC6265string(makeHTTPCookieObject(url, cookie), sameSite);
} catch (Exception e) {
promise.reject(e);
return;
Expand Down Expand Up @@ -301,7 +302,7 @@ private WritableMap createCookieData(HttpCookie cookie) {
* For our purposes RFC 6265 is the right way to go.
* This is a convenience method to give us the right formatting.
*/
private String toRFC6265string(HttpCookie cookie) {
private String toRFC6265string(HttpCookie cookie, String sameSite) {
StringBuilder builder = new StringBuilder();

builder.append(cookie.getName())
Expand Down Expand Up @@ -336,6 +337,10 @@ private String toRFC6265string(HttpCookie cookie) {
builder.append("; httponly");
}

if (sameSite != null) {
builder.append("; samesite=").append(sameSite);
}

return builder.toString();
}

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module '@react-native-cookies/cookies' {
expires?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: string;
}

export interface Cookies {
Expand Down
7 changes: 7 additions & 0 deletions ios/RNCookieManagerIOS/RNCookieManagerIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ -(NSHTTPCookie *)makeHTTPCookieObject:(NSURL *)url
NSDate *expires = [RCTConvert NSDate:props[@"expires"]];
NSNumber *secure = [RCTConvert NSNumber:props[@"secure"]];
NSNumber *httpOnly = [RCTConvert NSNumber:props[@"httpOnly"]];
NSNumber *sameSite = [RCTConvert NSString:props[@"sameSite"]];

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:name forKey:NSHTTPCookieName];
Expand Down Expand Up @@ -315,6 +316,9 @@ -(NSHTTPCookie *)makeHTTPCookieObject:(NSURL *)url
if ([httpOnly boolValue]) {
[cookieProperties setObject:httpOnly forKey:@"HttpOnly"];
}
if (!isEmpty(sameSite)) {
[cookieProperties setObject:sameSite forKey:@"SameSite"];
}

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];

Expand All @@ -328,6 +332,9 @@ -(NSDictionary *)createCookieData:(NSHTTPCookie *)cookie
[cookieData setObject:cookie.value forKey:@"value"];
[cookieData setObject:cookie.path forKey:@"path"];
[cookieData setObject:cookie.domain forKey:@"domain"];
if (!isEmpty(cookie.sameSitePolicy)) {
[cookieData setObject:cookie.sameSitePolicy forKey:@"sameSite"];
}
[cookieData setObject:[NSString stringWithFormat:@"%@", @(cookie.version)] forKey:@"version"];
if (!isEmpty(cookie.expiresDate)) {
[cookieData setObject:[self.formatter stringFromDate:cookie.expiresDate] forKey:@"expires"];
Expand Down