1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! Cross-platform cookie management for Dioxus fullstack applications.
//!
//! Dioxus apps can target web, desktop, iOS, and Android from a single codebase.
//! Native platforms lack built-in cookie storage—when a server function sets a cookie,
//! native apps silently discard it. **dioxus-cookie** provides a unified cookie API that
//! works across all supported platforms.
//!
//! # Quick Start
//!
//! ```rust,ignore
//! fn main() {
//! dioxus_cookie::init(); // Call before dioxus::launch()
//! dioxus::launch(App);
//! }
//!
//! #[server]
//! async fn login(credentials: Credentials) -> Result<(), ServerFnError> {
//! dioxus_cookie::set("session", &token, &CookieOptions::default())?;
//! Ok(())
//! }
//! ```
//!
//! # Platform Behavior
//!
//! | Platform | Storage |
//! |----------|---------|
//! | Server | HTTP `Set-Cookie` headers |
//! | Browser | `document.cookie` |
//! | Desktop | System keyring |
//! | iOS | Keychain |
//! | Android | KeyStore (default) or encrypted file (`android-file` feature) |
//!
//! # Features
//!
//! - `server` — Server-side cookie handling via HTTP headers
//! - `desktop` — Desktop platforms with system keyring storage
//! - `mobile` — iOS/Android with Keychain/KeyStore storage
//! - `android-file` — Force encrypted file storage on Android (skip KeyStore)
//! - `mobile-sim` — Mobile + file fallback for simulator/emulator development
//! - `file-store` — Encrypted file fallback (see security note below)
//!
//! # File Storage Fallback
//!
//! The `file-store` feature provides encrypted file-based storage for environments
//! where the system keychain is unavailable (iOS Simulator, Android Emulator,
//! Linux without D-Bus, CI/CD pipelines, Docker containers).
//!
//! **Security limitations:**
//! - **Debug builds only** — automatically disabled in release builds
//! - **Obfuscation, not protection** — deters casual inspection but does not
//! protect against local attackers with file system access
//! - **Not for production** — production apps must use real keychain storage
pub use *;
}};
}
/// Initialize the cookie system.
///
/// Call this **before** `dioxus::launch()` in your `main()` function.
/// This sets up the platform-appropriate cookie storage backend.
///
/// - **Server**: No-op (cookies handled via HTTP headers)
/// - **Browser**: No-op (cookies handled via `document.cookie`)
/// - **Desktop/Mobile**: Initializes system keyring and configures HTTP client
///
/// # Example
///
/// ```rust,ignore
/// fn main() {
/// dioxus_cookie::init();
/// dioxus::launch(App);
/// }
/// ```
///
/// # Note
///
/// If called after `dioxus::launch()` has already initialized the HTTP client,
/// the custom cookie store will not be used. Always call `init()` first.
/// Retrieves a cookie value by name.
///
/// Returns `None` if:
/// - The cookie doesn't exist
/// - The cookie is `HttpOnly` (blocked for security)
/// - The cookie has expired
///
/// # Example
///
/// ```rust,ignore
/// if let Some(theme) = dioxus_cookie::get("theme") {
/// println!("User prefers: {}", theme);
/// }
/// ```
///
/// # HttpOnly Cookies
///
/// This function respects `HttpOnly` just like browsers do—it returns `None`
/// for HttpOnly cookies to prevent client-side access. Use [`get_internal`]
/// only for server-side session restoration.
/// Retrieves a cookie value, bypassing the `HttpOnly` restriction.
///
/// **Warning**: Only use this for server-side operations like session restoration
/// on native platforms. Never expose HttpOnly cookie values to user-facing code.
///
/// # Example
///
/// ```rust,ignore
/// // In app initialization, restore session from stored cookie
/// if let Some(token) = dioxus_cookie::get_internal("session") {
/// restore_session(&token).await;
/// }
/// ```
/// Sets a cookie with the given name, value, and options.
///
/// # Arguments
///
/// * `name` — Cookie name (should be alphanumeric with hyphens/underscores)
/// * `value` — Cookie value (will be URL-encoded automatically)
/// * `options` — Cookie attributes (expiration, security flags, etc.)
///
/// # Example
///
/// ```rust,ignore
/// use dioxus_cookie::{CookieOptions, SameSite};
/// use std::time::Duration;
///
/// #[server]
/// async fn login(credentials: Credentials) -> Result<User, ServerFnError> {
/// let user = authenticate(credentials).await?;
///
/// dioxus_cookie::set("session", &user.token, &CookieOptions {
/// max_age: Some(Duration::from_secs(86400 * 7)),
/// http_only: true,
/// secure: true,
/// same_site: SameSite::Strict,
/// path: "/".to_string(),
/// })?;
///
/// Ok(user)
/// }
/// ```
///
/// # Platform Behavior
///
/// - **Server**: Sets `Set-Cookie` HTTP header in the response
/// - **Browser**: Writes to `document.cookie`
/// - **Desktop/Mobile**: Stores in system keyring
/// Deletes a cookie by name.
///
/// # Example
///
/// ```rust,ignore
/// #[server]
/// async fn logout() -> Result<(), ServerFnError> {
/// dioxus_cookie::clear("session")?;
/// Ok(())
/// }
/// ```
///
/// # Platform Behavior
///
/// - **Server**: Sets cookie with immediate expiration
/// - **Browser**: Removes from `document.cookie`
/// - **Desktop/Mobile**: Removes from system keyring
/// Lists names of all accessible cookies.
///
/// On native platforms, returns only non-HttpOnly cookies (matching browser behavior).
/// On the server, returns all cookies (HttpOnly info not available in request headers).
/// Expired cookies are automatically excluded on native platforms.
///
/// # Example
///
/// ```rust,ignore
/// let names = dioxus_cookie::list_names();
/// for name in names {
/// println!("Cookie: {}", name);
/// }
/// ```
/// Returns the active storage backend type.
///
/// Useful for debugging and diagnostics.
///
/// # Returns
///
/// - `"server"` — HTTP headers (server-side)
/// - `"keychain"` — System keyring (desktop/mobile)
/// - `"file"` — Encrypted file fallback (iOS Simulator, debug only)
/// - `"browser"` — `document.cookie` (WASM)
/// - `"stub"` — No-op implementation
/// - `"uninitialized"` — [`init`] not yet called