How to clear cookies with axum #1940
-
I am doing cookie based authentication with leptos and axum, and for the logout server function I want to just clear all the cookies. So far I have not been successful #[server(Logout, "/api", "GetJson", "logout")]
pub async fn logout() -> Result<(), ServerFnError> {
let response = expect_context::<leptos_axum::ResponseOptions>();
// First attempt: Just setting the SET_COOKIE header to an empty string.
// This does nothing
response.insert_header(header::SET_COOKIE, HeaderValue::from_str("").unwrap());
// Second attempt: This just sets the mycookie header to an empty string
let empty_cookie = Cookie::build("mycookie", "")
.path("/")
.finish();
response.insert_header(header::SET_COOKIE, HeaderValue::from_str(&empty_cookie.to_string()).unwrap());
// Third attempt: This makes the page try and download the response
// Perhaps it should inherit some headers from the response
response.overwrite(ResponseParts::default());
Ok(())
}
#[component]
fn LogoutComponent() -> impl IntoView {
view! {
<form method="GET" action="/api/logout">
<button type="submit">"Log out"</button>
</form>
}
} I think I am doing something wrong, but I just can't figure it out |
Beta Was this translation helpful? Give feedback.
Answered by
gbj
Oct 26, 2023
Replies: 1 comment 1 reply
-
As I understand it, setting an empty value with an |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
SorenHolstHansen
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As I understand it, setting an empty value with an
Expires
field in the past is the standard way to delete a cookie. See here.