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
}