@preeternal/react-native-cookie-manager
v6.3.1
Published
A modern, New Architecture–ready Cookie Manager for React Native.A drop-in replacement for @react-native-cookies/cookies, fully rewritten with TypeScript, TurboModules, and platform-native implementations for iOS and Android. Supports reading, writing, an
Downloads
14,800
Readme
@preeternal/react-native-cookie-manager
A modern, New Architecture–ready Cookie Manager for React Native. This is a drop-in replacement for @react-native-cookies/cookies, rewritten with TypeScript, TurboModules, and platform-native implementations for iOS (Swift) and Android (Kotlin).
Upstream / credits
This package is based on the public API and behavior of @react-native-cookies/cookies. Big thanks to the upstream maintainers and contributors for the original implementation and long-term work on the project.
Installation
Using Bun
bun add @preeternal/react-native-cookie-managerUsing yarn
yarn add @preeternal/react-native-cookie-managerUsing npm
npm install @preeternal/react-native-cookie-managerThen install iOS pods:
cd ios && bundle exec pod installSupports both old (bridged) and New Architecture (TurboModule) builds out of the box. Works in bare RN apps and in Expo Dev Builds (custom native build).
Usage
import CookieManager from '@preeternal/react-native-cookie-manager';
// Set a cookie
try {
await CookieManager.set('https://example.com', {
name: 'session',
value: 'abc123',
domain: 'example.com',
path: '/',
secure: true,
httpOnly: true,
});
// Set cookies from a Set-Cookie header
await CookieManager.setFromResponse(
'https://example.com',
'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly'
);
// Get cookies for a URL
const cookies = await CookieManager.get('https://example.com');
// iOS only: get all cookies
const allCookies = await CookieManager.getAll();
// Clear by name (iOS only)
await CookieManager.clearByName('https://example.com', 'session');
// Clear everything
await CookieManager.clearAll();
// Android only: persist to storage
await CookieManager.flush();
// Android only: remove session cookies (no expires)
await CookieManager.removeSessionCookies();
} catch (err) {
console.warn('Cookie operation failed', err);
}API (compatible with @react-native-cookies/cookies)
set(url, cookie, useWebKit?)setFromResponse(url, cookieHeader)get(url, useWebKit?)getFromResponse(url)clearAll(useWebKit?)flush()— AndroidremoveSessionCookies()— AndroidgetAll(useWebKit?)— iOSclearByName(url, name, useWebKit?)— iOS
useWebKit applies only to iOS (switches to WKHTTPCookieStore); on Android it is ignored because WebView and native share a single cookie store.
WebKit on iOS
- iOS has two stores:
NSHTTPCookieStorage(used by URLSession) andWKHTTPCookieStore(used by WKWebView /react-native-webview). - Pass
useWebKit: truewhen you need cookies to sync with WKWebView. For network-only flows, omit it to useNSHTTPCookieStorage. - If your app mixes both (native requests and embedded web), call the same operation twice: once with
useWebKit: true(for WKWebView), once withuseWebKit: false(for URLSession). - On Android the flag is ignored; WebView and native use the same store.
Cookie shape
type Cookie = {
name: string;
value: string;
path?: string;
domain?: string;
version?: string;
expires?: string; // ISO 8601 string, e.g. 2015-05-30T12:30:00.00-05:00
secure?: boolean;
httpOnly?: boolean;
};Contributing
License
MIT
Made with create-react-native-library
