axum-sessions icon indicating copy to clipboard operation
axum-sessions copied to clipboard

Cannot retrieve records in handlers

Open AppyCat opened this issue 3 years ago • 8 comments

Hi, I am able to insert a record into session in one handler but unable to retrieve it in another. Could you:

  1. Put some docs with a how to example
  2. Let me know how to set the session and store as a layer in Axum
  3. How to use Redis as the session store

Since docs are not present in this crate currently, I proceeded with docs from async-sessions and got the following errors:

90 | let cookie_value = store.store_session(session).await; | ^^^^^^^^^^^^^ method not found in Result<RedisSessionStore, redis::types::RedisError>

error[E0599]: no method named load_session found for enum Result in the current scope --> src/main.rs:91:29 | 91 | let mut session = store.load_session("test-cookie".parse().unwrap()).await.unwrap(); | ^^^^^^^^^^^^ method not found in Result<RedisSessionStore, redis::types::RedisError>

From the following code:

async fn do_something(Extension(session): Extension<Session>) -> Json<Value> {
    let store = RedisSessionStore::new("redis://127.0.0.1/");
    let cookie_value = store.store_session(session).await;
    let mut session = store.load_session("test-cookie".parse().unwrap()).await.unwrap();

    let user_id: String = session.get::<String>("user_id").unwrap();
    Json(json!({ "data": user_id }))
}

Appreciate any help on this front.

AppyCat avatar Jun 30 '22 14:06 AppyCat

Hi there, have you had a look at the Crate docs already? This example in particular might be helpful.

There isn't an example of Redis specifically, however I'd be happy to accept a PR with an implementation in examples/. I can also share how I'm using the SQLx variation:

    let store = PostgresSessionStore::new(&config.database_url)
        .await
        .expect("Could not connect to database.");
    store.migrate().await.unwrap();

    let service = ServiceBuilder::new().layer(
        SessionLayer::new(store, config.app_secret.as_bytes())
            .with_same_site_policy(SameSite::Lax)
            .with_secure(false),
    );

maxcountryman avatar Jun 30 '22 14:06 maxcountryman

Thanks for the links, I did view the docs and use the example.

However with session_layer as the layer, it's not storing a record set in one handler which is retrievable in another handler.

Perhaps it may be an idea to doc an example of an insert in one handler and a get in another handler.

Redis is generally more widely used to store sessions, so if you could doc a Redis example, insert & get in separate handlers, that would be awesome.

AppyCat avatar Jun 30 '22 14:06 AppyCat

I have a relatively complex project with a redis session using this crate. When I'll have a little more free time, I'll submit a PR with a basic working example. The redis part is not that complicated imo, so basically I just followed the docs.

Edit: There's one little problem with it though; I had to fork async_redis_session and change it to use tokio instead of async-std.

Ptrskay3 avatar Jun 30 '22 15:06 Ptrskay3

Edit: There's one little problem with it though; I had to fork async_redis_session and change it to use tokio instead of async-std.

Oh that's too bad: async-sqlx-session allows you to configure the async runtime with feature flags. I wonder if async-redis-session would be open to a similar approach?

maxcountryman avatar Jun 30 '22 18:06 maxcountryman

That would be ideal if async-redis-session enables Tokio as a feature flag. Thanks for all your input and help.

AppyCat avatar Jun 30 '22 20:06 AppyCat

@AppyCat I have a redis example here. I'm not going to submit a PR, as the async_redis_session crate still uses async-std for the Redis connection, and if you run that example, it'll spin up both tokio and async-std. I personally have my own fork of async_redis_session where I patched to use get_tokio_connection instead of get_async_std_connection.

Ptrskay3 avatar Aug 04 '22 18:08 Ptrskay3

I also added a sqlx example for reference.

Given that async-sqlx-session and async-redis-session share maintainers, I wonder if there's appetite for incorporating upstream changes to support tokio with the redis variant.

maxcountryman avatar Aug 08 '22 16:08 maxcountryman

@AppyCat we've released tower-sessions 0.1.0 which includes Redis support.

maxcountryman avatar Sep 25 '23 10:09 maxcountryman