Check if a session is set Actix

Hello

This is the relevant code:

async fn test_post_req(form: web::Form<TestPostData>, session: Session) -> Result<String> {

    if session.get::<String>("rofl")?.is_none() {
        println!("No sessions");
        session.set("rofl", "lol")?;

    } else {
        let x = session.get::<String>("rofl")?;
        println!("Session found: {:?}", x);
    }
    Ok(format!("{}", form.id))
}

I don't know how to check if a specific session is set in Actix. A session is wrapped in an Option<> so I thought than when a session wouldn't exist it'd give None. But this isn't the case. The if-statement always executes on each request. It should only be executed after the first request...

Full code:

use actix_web::{web, get, middleware, Result, Responder, HttpResponse, HttpServer, App};
use actix_session::{Session, CookieSession};
use actix_cors::Cors;
use serde::Deserialize;

#[derive(Deserialize)]
struct TestPostData {
    id: u8,
    str: String,
}

async fn test_post_req(form: web::Form<TestPostData>, session: Session) -> Result<String> {

    if session.get::<String>("rofl")?.is_none() {
        println!("No sessions");
        session.set("rofl", "lol")?;

    } else {
        let x = session.get::<String>("rofl")?;
        println!("Session found: {:?}", x);
    }
    Ok(format!("{}", form.id))
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
            App::new()
                .wrap(
                    Cors::default()
                        .allowed_origin("http://ajaxrust.local")
                )
                .wrap(
                    CookieSession::signed(&[0; 32])
                        .secure(false)
                )
                .service(
                    web::resource("/")
                    .route(web::post().to(test_post_req))
                )
        })
        .bind("127.0.0.1:8088")?
        .run()
        .await
}

Even the example of Actix fails:

use actix_web::{web, get, middleware, Result, Responder, HttpResponse, HttpServer, App};
use actix_session::{Session, CookieSession};
use actix_cors::Cors;
use serde::Deserialize;

#[derive(Deserialize)]
struct TestPostData {
    id: u8,
    str: String,
}

async fn test_post_req(form: web::Form<TestPostData>, session: Session) -> Result<String> {

    if let Some(count) = session.get::<i32>("counter")? {
           session.set("counter", count + 1)?;
       } else {
           session.set("counter", 1)?;
       }

       println!("{:?}", session.get::<i32>("counter")?);
    Ok("".to_string())

}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
            App::new()
                .wrap(
                    Cors::default()
                        .allowed_origin("http://ajaxrust.local")
                )
                .wrap(
                    CookieSession::signed(&[0; 32])
                        .secure(false)
                )
                .service(
                    web::resource("/")
                    .route(web::post().to(test_post_req))
                )
        })
        .bind("127.0.0.1:8088")?
        .run()
        .await
}

The state of the session is lost.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.