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

Cannot retrieve records in handlers #3

Open
AppyCat opened this issue Jun 30, 2022 · 8 comments
Open

Cannot retrieve records in handlers #3

AppyCat opened this issue Jun 30, 2022 · 8 comments

Comments

@AppyCat
Copy link

AppyCat commented Jun 30, 2022

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.

@maxcountryman
Copy link
Owner

maxcountryman commented Jun 30, 2022

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),
    );

@AppyCat
Copy link
Author

AppyCat commented Jun 30, 2022

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.

@Ptrskay3
Copy link
Contributor

Ptrskay3 commented Jun 30, 2022

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.

@maxcountryman
Copy link
Owner

maxcountryman commented Jun 30, 2022

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?

@AppyCat
Copy link
Author

AppyCat commented Jun 30, 2022

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

@Ptrskay3
Copy link
Contributor

Ptrskay3 commented Aug 4, 2022

@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.

@maxcountryman
Copy link
Owner

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
Copy link
Owner

maxcountryman commented Sep 25, 2023

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

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

3 participants