Sessions not storing data

I'm trying to store some simple data in a tower_sessions::Session using SessionManagerLayer middleware but I can't get the sessions to actually work the session id:s are None in one endpoint and something in another but for it to work they ofcourse need to be the same so that the data put into the session can be retrieved, my code is available at GitHub - fitinghof/WhatAnime
I am totally new to both rust and any sort of network stuff. I have been banging my head against the wall for hours now trying to figure this out but I'm totally lost, any help would be apreciated! The main problem I'm trying to solve now is the login chain for spotify that is initiated through the .../login route, this would then redirect to spotify that will inturn redirect back to the .../callback route. Here my login function should store a state in the session that should then be retrieved by the callback function, but I can't get the sessions to actually work for that.

1 Like

Here is a scaled down version of the same problem:

use axum::response::IntoResponse;
use axum::{
    Router,
    routing::get,
};
use tower_http::cors::{Any, CorsLayer};
use tower_sessions::{cookie::{time::Duration, SameSite}, Expiry, MemoryStore, Session, SessionManagerLayer};

pub async fn login(session: Session) -> impl IntoResponse {
    session.insert("key", "value").await.unwrap();
    session.save().await.unwrap();
    return axum::response::Redirect::to("http://127.0.0.1:8000/callback");
}

pub async fn callback(session: Session) -> impl IntoResponse {
    session.load().await.unwrap();
    let value = session.get::<String>("key").await.unwrap();
    if value.as_ref().is_none_or(|value|value != "value") {
        return axum::http::StatusCode::BAD_GATEWAY.into_response();
    }
    let ret = value.unwrap();
    println!("{}", &ret);
    ret.into_response()
}

#[tokio::main]
async fn main() {
    let session_store = MemoryStore::default();
    let session_layer = SessionManagerLayer::new(session_store)
        .with_secure(false)
        .with_always_save(true)
        .with_expiry(Expiry::OnInactivity(Duration::seconds(10)))
        .with_secure(false)
        .with_same_site(SameSite::None);

    let app = Router::new()
        .route("/login", get(login))
        .route("/callback", get(callback))
        .layer(session_layer)
        .layer(CorsLayer::new().allow_origin(Any));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:8000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap()
}