diff --git a/README.md b/README.md index a560361..d4405c7 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ export interface Cookie { expires?: string; secure?: boolean; httpOnly?: boolean; + sameSite?: string; } export interface Cookies { diff --git a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java index f526d03..73d3be7 100644 --- a/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java +++ b/android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java @@ -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; @@ -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()) @@ -336,6 +337,10 @@ private String toRFC6265string(HttpCookie cookie) { builder.append("; httponly"); } + if (sameSite != null) { + builder.append("; samesite=").append(sameSite); + } + return builder.toString(); } diff --git a/index.d.ts b/index.d.ts index b2977de..06701f7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,6 +8,7 @@ declare module '@react-native-cookies/cookies' { expires?: string; secure?: boolean; httpOnly?: boolean; + sameSite?: string; } export interface Cookies { diff --git a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m index 0e76dba..277bed4 100644 --- a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m +++ b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m @@ -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]; @@ -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]; @@ -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"];