Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting PostgresSessionStore between server restarts #38

Open
abcd-ca opened this issue Oct 11, 2023 · 0 comments
Open

Persisting PostgresSessionStore between server restarts #38

abcd-ca opened this issue Oct 11, 2023 · 0 comments

Comments

@abcd-ca
Copy link

abcd-ca commented Oct 11, 2023

Hi, I have an Axum server and was using MemoryStore. I just swapped that for PostgresSessionStore. I can see that between browser page reloads that my session is persisting but when I quit my server and restart it then reload the browser again, it seems the session is lost. I can see the async_sessions table in my db has been created.

Here is how I'm configuring it,

// session cookie ------ start
    let store = PostgresSessionStore::new(&db_url)
        .await
        .map_err(|e| {
            eprintln!("Database error: {:?}", e);
            RestAPIError::InternalServerError
        })
        .unwrap();
    store.migrate().await.unwrap();
    store.spawn_cleanup_task(Duration::from_secs(60 * 60));
    
    // I couldn't see how three lines from the async-sqlx-sesssion example were needed with Axum. 
    // let mut session = Session::new();
    // let cookie_value = store.store_session(session).await.unwrap().unwrap();
    // let session = store.load_session(cookie_value).await.unwrap();

    let secret = random::<[u8; 128]>();
    let session_layer = SessionLayer::new(store, &secret)
        .with_secure(true)
        .with_same_site_policy(SameSite::Strict);
    // session cookie ------ end
    
    ...
    
    let routes_all = Router::new()
        ...
        .layer(session_layer)
        .layer(Extension(app_state))
        .fallback_service(routes_static());

In my login route I just update the session like this:

async fn api_login(
    mut session: WritableSession,
    Extension(app_state): Extension<Arc<AppState>>,
    payload: Json<LoginRequestPostPayload>,
) -> RestAPIResult<Json<StandardSuccessResponseData>> {
          
           ...

           session
                .insert("token", payload.token.to_string())
                .map_err(|_| RestAPIError::UpdateSessionFail)?;

            Ok(Json(StandardSuccessResponseData::new()))
}

Would you please help me identify the gap?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant