How to update a private cookie in a middleware #2463
-
SummaryHi there, I'm trying to implement a „remember me” functionality. For this I'd like to update a private cookie with every request. I figured that a middleware would be the best option to do this, but I can't get the cookie to be updated. This is my attempt: pub async fn remember_me(
State(state): State<AppState>,
mut auth: AuthSession,
jar: PrivateCookieJar,
request: Request,
next: Next,
) -> Response {
if let Some(cookie) = jar.get("remember_me") {
if let Ok(user_id) = cookie.value().parse::<i64>() {
let now = cookie::time::OffsetDateTime::now_utc() + cookie::time::Duration::days(30);
let cookie = Cookie::build(("remember_me", user_id.to_string()))
.expires(now)
.http_only(true)
.secure(SECURE)
.path("/")
.domain(std::env::var("COOKIE_DOMAIN").unwrap_or_default())
.same_site(cookie::SameSite::Strict)
.build();
jar.add(cookie);
if auth.user.is_none() {
let user = users::get_user(user_id, &state.pool).await.ok().flatten();
if let Some(user) = user {
auth.login(&user).await.ok();
}
}
}
}
next.run(request).await
} What am I doing wrong? Best regards, axum version0.7.2 |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Dec 30, 2023
Replies: 1 comment 5 replies
-
From the docs:
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is, but you need to return the updated jar along with the response from
next.run(...)
.