1 | # CapacitorCookies
|
2 |
|
3 | The Capacitor Cookies API provides native cookie support via patching `document.cookie` to use native libraries. It also provides methods for modifying cookies at a specific url. This plugin is bundled with `@capacitor/core`.
|
4 |
|
5 | ## Configuration
|
6 |
|
7 | By default, the patching of `document.cookie` to use native libraries is disabled.
|
8 | If you would like to enable this feature, modify the configuration below in the `capacitor.config` file.
|
9 |
|
10 | | Prop | Type | Description | Default |
|
11 | | ------------- | -------------------- | ------------------------------------------------------------------------- | ------------------ |
|
12 | | **`enabled`** | <code>boolean</code> | Enable the patching of `document.cookie` to use native libraries instead. | <code>false</code> |
|
13 |
|
14 | ### Example Configuration
|
15 |
|
16 | In `capacitor.config.json`:
|
17 |
|
18 | ```json
|
19 | {
|
20 | "plugins": {
|
21 | "CapacitorCookies": {
|
22 | "enabled": true
|
23 | }
|
24 | }
|
25 | }
|
26 | ```
|
27 |
|
28 | In `capacitor.config.ts`:
|
29 |
|
30 | ```ts
|
31 | import { CapacitorConfig } from '@capacitor/cli';
|
32 |
|
33 | const config: CapacitorConfig = {
|
34 | plugins: {
|
35 | CapacitorCookies: {
|
36 | enabled: true,
|
37 | },
|
38 | },
|
39 | };
|
40 |
|
41 | export default config;
|
42 | ```
|
43 |
|
44 | ## Example
|
45 |
|
46 | ```typescript
|
47 | import { CapacitorCookies } from '@capacitor/core';
|
48 |
|
49 | const getCookies = () => {
|
50 | return document.cookie;
|
51 | };
|
52 |
|
53 | const setCookie = () => {
|
54 | document.cookie = key + '=' + value;
|
55 | };
|
56 |
|
57 | const setCapacitorCookie = async () => {
|
58 | await CapacitorCookies.setCookie({
|
59 | url: 'http://example.com',
|
60 | key: 'language',
|
61 | value: 'en',
|
62 | });
|
63 | };
|
64 |
|
65 | const deleteCookie = async () => {
|
66 | await CapacitorCookies.deleteCookie({
|
67 | url: 'https://example.com',
|
68 | key: 'language',
|
69 | });
|
70 | };
|
71 |
|
72 | const clearCookiesOnUrl = async () => {
|
73 | await CapacitorCookies.clearCookies({
|
74 | url: 'https://example.com',
|
75 | });
|
76 | };
|
77 |
|
78 | const clearAllCookies = async () => {
|
79 | await CapacitorCookies.clearAllCookies();
|
80 | };
|
81 | ```
|
82 |
|
83 | ## Third Party Cookies on iOS
|
84 |
|
85 | As of iOS 14, you cannot use 3rd party cookies by default. Add the following lines to your Info.plist file to get better support for cookies on iOS. You can add up to 10 domains.
|
86 |
|
87 | ```xml
|
88 | <key>WKAppBoundDomains</key>
|
89 | <array>
|
90 | <string>www.mydomain.com</string>
|
91 | <string>api.mydomain.com</string>
|
92 | <string>www.myothercooldomain.com</string>
|
93 | </array>
|
94 | ```
|
95 |
|
96 | ## API
|
97 |
|
98 | <docgen-index>
|
99 |
|
100 | * [`getCookies(...)`](#getcookies)
|
101 | * [`setCookie(...)`](#setcookie)
|
102 | * [`deleteCookie(...)`](#deletecookie)
|
103 | * [`clearCookies(...)`](#clearcookies)
|
104 | * [`clearAllCookies()`](#clearallcookies)
|
105 | * [Interfaces](#interfaces)
|
106 | * [Type Aliases](#type-aliases)
|
107 |
|
108 | </docgen-index>
|
109 |
|
110 | <docgen-api>
|
111 |
|
112 |
|
113 | ### getCookies(...)
|
114 |
|
115 | ```typescript
|
116 | getCookies(options?: GetCookieOptions) => Promise<HttpCookieMap>
|
117 | ```
|
118 |
|
119 | | Param | Type |
|
120 | | ------------- | ------------------------------------------------------------- |
|
121 | | **`options`** | <code><a href="#getcookieoptions">GetCookieOptions</a></code> |
|
122 |
|
123 | **Returns:** <code>Promise<<a href="#httpcookiemap">HttpCookieMap</a>></code>
|
124 |
|
125 | --------------------
|
126 |
|
127 |
|
128 | ### setCookie(...)
|
129 |
|
130 | ```typescript
|
131 | setCookie(options: SetCookieOptions) => Promise<void>
|
132 | ```
|
133 |
|
134 | Write a cookie to the device.
|
135 |
|
136 | | Param | Type |
|
137 | | ------------- | ------------------------------------------------------------- |
|
138 | | **`options`** | <code><a href="#setcookieoptions">SetCookieOptions</a></code> |
|
139 |
|
140 | --------------------
|
141 |
|
142 |
|
143 | ### deleteCookie(...)
|
144 |
|
145 | ```typescript
|
146 | deleteCookie(options: DeleteCookieOptions) => Promise<void>
|
147 | ```
|
148 |
|
149 | Delete a cookie from the device.
|
150 |
|
151 | | Param | Type |
|
152 | | ------------- | ------------------------------------------------------------------- |
|
153 | | **`options`** | <code><a href="#deletecookieoptions">DeleteCookieOptions</a></code> |
|
154 |
|
155 | --------------------
|
156 |
|
157 |
|
158 | ### clearCookies(...)
|
159 |
|
160 | ```typescript
|
161 | clearCookies(options: ClearCookieOptions) => Promise<void>
|
162 | ```
|
163 |
|
164 | Clear cookies from the device at a given URL.
|
165 |
|
166 | | Param | Type |
|
167 | | ------------- | ----------------------------------------------------------------- |
|
168 | | **`options`** | <code><a href="#clearcookieoptions">ClearCookieOptions</a></code> |
|
169 |
|
170 | --------------------
|
171 |
|
172 |
|
173 | ### clearAllCookies()
|
174 |
|
175 | ```typescript
|
176 | clearAllCookies() => Promise<void>
|
177 | ```
|
178 |
|
179 | Clear all cookies on the device.
|
180 |
|
181 | --------------------
|
182 |
|
183 |
|
184 | ### Interfaces
|
185 |
|
186 |
|
187 | #### HttpCookieMap
|
188 |
|
189 |
|
190 | #### HttpCookie
|
191 |
|
192 | | Prop | Type | Description |
|
193 | | ----------- | ------------------- | ------------------------ |
|
194 | | **`url`** | <code>string</code> | The URL of the cookie. |
|
195 | | **`key`** | <code>string</code> | The key of the cookie. |
|
196 | | **`value`** | <code>string</code> | The value of the cookie. |
|
197 |
|
198 |
|
199 | #### HttpCookieExtras
|
200 |
|
201 | | Prop | Type | Description |
|
202 | | ------------- | ------------------- | -------------------------------- |
|
203 | | **`path`** | <code>string</code> | The path to write the cookie to. |
|
204 | | **`expires`** | <code>string</code> | The date to expire the cookie. |
|
205 |
|
206 |
|
207 | ### Type Aliases
|
208 |
|
209 |
|
210 | #### GetCookieOptions
|
211 |
|
212 | <code><a href="#omit">Omit</a><<a href="#httpcookie">HttpCookie</a>, 'key' | 'value'></code>
|
213 |
|
214 |
|
215 | #### Omit
|
216 |
|
217 | Construct a type with the properties of T except for those in type K.
|
218 |
|
219 | <code><a href="#pick">Pick</a><T, <a href="#exclude">Exclude</a><keyof T, K>></code>
|
220 |
|
221 |
|
222 | #### Pick
|
223 |
|
224 | From T, pick a set of properties whose keys are in the union K
|
225 |
|
226 | <code>{
[P in K]: T[P];
}</code>
|
227 |
|
228 |
|
229 | #### Exclude
|
230 |
|
231 | <a href="#exclude">Exclude</a> from T those types that are assignable to U
|
232 |
|
233 | <code>T extends U ? never : T</code>
|
234 |
|
235 |
|
236 | #### SetCookieOptions
|
237 |
|
238 | <code><a href="#httpcookie">HttpCookie</a> & <a href="#httpcookieextras">HttpCookieExtras</a></code>
|
239 |
|
240 |
|
241 | #### DeleteCookieOptions
|
242 |
|
243 | <code><a href="#omit">Omit</a><<a href="#httpcookie">HttpCookie</a>, 'value'></code>
|
244 |
|
245 |
|
246 | #### ClearCookieOptions
|
247 |
|
248 | <code><a href="#omit">Omit</a><<a href="#httpcookie">HttpCookie</a>, 'key' | 'value'></code>
|
249 |
|
250 | </docgen-api> |
\ | No newline at end of file |