Skip to content

How can i mutate my cookie in a middleware #3101

Answered by robjtede
Necoo33 asked this question in Q&A
Discussion options

You must be logged in to vote
use actix_web_lab::middleware::{from_fn, Next};

             App::new()
                 // ...
                .wrap(from_fn(refresh_cookies))
use actix_web::{
    body::MessageBody,
    cookie::Cookie,
    dev::{ServiceRequest, ServiceResponse},
    Error,
};
use actix_web_lab::middleware::{from_fn, Next};

async fn refresh_cookies(
    req: ServiceRequest,
    next: Next<impl MessageBody>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
    let auth_cookie = req.cookie("web-auth").clone();

    let mut res = next.call(req).await?;

    if let Some(cookie) = auth_cookie {
        let refreshed_cookie = Cookie::build(cookie.name(), cookie.value())
            .domain("example.com")

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Necoo33
Comment options

Answer selected by Necoo33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants