set

Function set 

Source
pub fn set(
    name: &str,
    value: &str,
    options: &CookieOptions,
) -> Result<(), CookieError>
Expand description

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

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