0

I am trying to create a security checker if the current request has a valid session cookie.

I already have this code:

        .wrap_fn(|rq, srv| {
            async move {
                let res = rq.cookie("sessionid").clone();
                if res.is_none() {
                    let mut resp = rq.into_response(HttpResponse::Unauthorized().finish());
                    return Ok(resp);
                } else {
                    let fut = srv.call(rq);
                    Ok(fut.await.unwrap())
                }
            }
        })

Unfortunately I get the following error message:

error: lifetime may not live long enough
  --> src/gpodder/routes.rs:37:13
   |
36 |           .wrap_fn(|rq, srv| {
   |                         ---- return type of closure `[async block@src/gpodder/routes.rs:37:13: 46:14]` contains a lifetime `'2`
   |                         |
   |                         has type `&'1 actix_web::scope::ScopeService`
37 | /             async move {
38 | |                 let res = rq.cookie("sessionid").clone();
39 | |                 if res.is_none() {
40 | |                     let mut resp = rq.into_response(HttpResponse::Unauthorized().finish());
...  |
45 | |                 }
46 | |             }
   | |_____________^ returning this value requires that `'1` must outlive `'2`

How could I solve this issue? If I put srv.call(rq) out of the async move block I get an error that rq is already borrowed.

Samuel
  • 547
  • 1
  • 3
  • 14

0 Answers0