Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Jul 9, 2024
1 parent 1990d5a commit e4286ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .zetch.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 31 additions & 25 deletions rust/bitbazaar/cookies/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,39 @@ pub fn set_cookie_raw(name: &str, value: &str, options: CookieOptions<'_>) {

use crate::prelude::*;

let axum_response = leptos::expect_context::<leptos_axum::ResponseOptions>();
let mut cookie = Cookie::build((name, value)).http_only(options.http_only);
if let Some(path) = options.path {
cookie = cookie.path(path);
}
if let Some(domain) = options.domain {
cookie = cookie.domain(domain);
}
if let Some(expires) = options.expires {
cookie = cookie.max_age(time::Duration::milliseconds(expires.num_milliseconds()));
}
if options.secure {
cookie = cookie.secure(true);
}
cookie = match options.same_site {
SameSite::Lax => cookie.same_site(axum_extra::extract::cookie::SameSite::Lax),
SameSite::Strict => cookie.same_site(axum_extra::extract::cookie::SameSite::Strict),
SameSite::None => cookie.same_site(axum_extra::extract::cookie::SameSite::None),
};

match http::HeaderValue::from_str(&cookie.to_string()).change_context(AnyErr) {
Ok(cookie) => {
axum_response.append_header(http::header::SET_COOKIE, cookie);
if let Some(resp_opts) = leptos::use_context::<leptos_axum::ResponseOptions>() {
let mut cookie = Cookie::build((name, value)).http_only(options.http_only);
if let Some(path) = options.path {
cookie = cookie.path(path);
}
Err(e) => {
record_exception("Failed to set cookie.", format!("{:?}", e));
if let Some(domain) = options.domain {
cookie = cookie.domain(domain);
}
if let Some(expires) = options.expires {
cookie = cookie.max_age(time::Duration::milliseconds(expires.num_milliseconds()));
}
if options.secure {
cookie = cookie.secure(true);
}
cookie = match options.same_site {
SameSite::Lax => cookie.same_site(axum_extra::extract::cookie::SameSite::Lax),
SameSite::Strict => cookie.same_site(axum_extra::extract::cookie::SameSite::Strict),
SameSite::None => cookie.same_site(axum_extra::extract::cookie::SameSite::None),
};

match http::HeaderValue::from_str(&cookie.to_string()).change_context(AnyErr) {
Ok(cookie) => {
resp_opts.append_header(http::header::SET_COOKIE, cookie);
}
Err(e) => {
record_exception("Failed to set cookie.", format!("{:?}", e));
}
}
} else {
record_exception(
"Couldn't get response options from leptos_axum::ResponseOptions context.",
"",
);
}
}
}
Expand Down

0 comments on commit e4286ba

Please sign in to comment.