Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@react-native-cookies/cookies
Advanced tools
@react-native-cookies/cookies is a React Native library that provides a simple API for managing cookies in your React Native applications. It allows you to set, get, and clear cookies, making it easier to handle authentication and session management in your mobile apps.
Set a Cookie
This feature allows you to set a cookie for a specific domain. The code sample demonstrates how to set a cookie with various attributes such as name, value, domain, path, version, and expiration date.
import { Cookies } from '@react-native-cookies/cookies';
Cookies.set('https://example.com', {
name: 'myCookie',
value: 'cookieValue',
domain: 'example.com',
path: '/',
version: '1',
expires: '2023-12-31T23:59:59.000Z'
});
Get Cookies
This feature allows you to retrieve all cookies for a specific domain. The code sample demonstrates how to get cookies and log them to the console.
import { Cookies } from '@react-native-cookies/cookies';
Cookies.get('https://example.com').then(cookies => {
console.log(cookies);
});
Clear Cookies
This feature allows you to clear all cookies. The code sample demonstrates how to clear all cookies and log a confirmation message to the console.
import { Cookies } from '@react-native-cookies/cookies';
Cookies.clearAll().then(() => {
console.log('All cookies cleared');
});
react-native-cookies is another library for managing cookies in React Native applications. It provides similar functionalities such as setting, getting, and clearing cookies. However, @react-native-cookies/cookies is a more modern and actively maintained alternative.
Cookie Manager for React Native
This module was ported from joeferraro/react-native-cookies. This would not exist without the work of the original author, Joe Ferraro.
@react-native-cookies/cookies
.@react-native-community/cookies
.✅ iOS
✅ Android
❌ Expo is working on their own cookie support (https://github.com/expo/expo/issues/6756)
Currently lacking support for Windows, macOS, and web. Support for these platforms will be created when there is a need for them. Starts with a posted issue.
yarn add @react-native-cookies/cookies
Then link the native iOS package
npx pod-install
A cookie object can have one of the following fields:
export interface Cookie {
name: string;
value: string;
path?: string;
domain?: string;
version?: string;
expires?: string;
secure?: boolean;
httpOnly?: boolean;
}
export interface Cookies {
[key: string]: Cookie;
}
import CookieManager from '@react-native-cookies/cookies';
// set a cookie
CookieManager.set('http://example.com', {
name: 'myCookie',
value: 'myValue',
domain: 'some domain',
path: '/',
version: '1',
expires: '2015-05-30T12:30:00.00-05:00'
}).then((done) => {
console.log('CookieManager.set =>', done);
});
*NB:* When no `domain` is specified, url host will be used instead.
*NB:* When no `path` is specified, an empty path `/` will be assumed.
// 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')
.then((success) => {
console.log('CookieManager.setFromResponse =>', success);
});
// Get cookies for a url
CookieManager.get('http://example.com')
.then((cookies) => {
console.log('CookieManager.get =>', cookies);
});
// list cookies (IOS ONLY)
CookieManager.getAll()
.then((cookies) => {
console.log('CookieManager.getAll =>', cookies);
});
// clear cookies
CookieManager.clearAll()
.then((success) => {
console.log('CookieManager.clearAll =>', success);
});
// clear a specific cookie by its name (IOS ONLY)
CookieManager.clearByName('http://example.com', 'cookie_name')
.then((success) => {
console.log('CookieManager.clearByName =>', success);
});
// flush cookies (ANDROID ONLY)
CookieManager.flush()
.then((success) => {
console.log('CookieManager.flush =>', success);
});
// Remove session cookies (ANDROID ONLY)
// Session cookies are cookies with no expires set. Android typically does not
// remove these, it is up to the developer to decide when to remove them.
// The return value is true if any session cookies were removed.
// iOS handles removal of session cookies automatically on app open.
CookieManager.removeSessionCookies()
.then((sessionCookiesRemoved) => {
console.log('CookieManager.removeSessionCookies =>', sessionCookiesRemoved);
});
React Native comes with a WebView component, which uses UIWebView on iOS. Introduced in iOS 8 Apple implemented the WebKit-Support with all the performance boost.
Prior to WebKit-Support, cookies would have been stored in NSHTTPCookieStorage
and sharedCookiesEnabled must be set on webviews to ensure access to them.
With WebKit-Support, cookies are stored in a separate webview store WKHTTPCookieStore
and not necessarily shared with other http requests. Caveat is that this store is available upon mounting the component but not necessarily prior so any attempts to set a cookie too early may result in a false positive.
To use WebKit-Support, you should be able to simply make advantage of the react-native-webview as is OR alternatively use the webview component like react-native-wkwebview.
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) |
import CookieManager from '@react-native-cookies/cookies';
const useWebKit = true;
// list cookies (IOS ONLY)
CookieManager.getAll(useWebKit)
.then((cookies) => {
console.log('CookieManager.getAll from webkit-view =>', cookies);
});
// clear cookies
CookieManager.clearAll(useWebKit)
.then((succcess) => {
console.log('CookieManager.clearAll from webkit-view =>', succcess);
});
// clear cookies with name (IOS ONLY)
CookieManager.clearByName('http://example.com', 'cookie name', useWebKit)
.then((succcess) => {
console.log('CookieManager.clearByName from webkit-view =>', succcess);
});
// Get cookies as a request header string
CookieManager.get('http://example.com', useWebKit)
.then((cookies) => {
console.log('CookieManager.get from webkit-view =>', cookies);
});
// set a cookie
const newCookie: = {
name: 'myCookie',
value: 'myValue',
domain: 'some domain',
path: '/',
version: '1',
expires: '2015-05-30T12:30:00.00-05:00'
};
CookieManager.set('http://example.com', newCookie, useWebKit)
.then((res) => {
console.log('CookieManager.set from webkit-view =>', res);
});
FAQs
Cookie Manager for React Native
The npm package @react-native-cookies/cookies receives a total of 117,138 weekly downloads. As such, @react-native-cookies/cookies popularity was classified as popular.
We found that @react-native-cookies/cookies demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.