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

Setting a layer or service #17

Closed
AppyCat opened this issue Jun 29, 2022 · 5 comments
Closed

Setting a layer or service #17

AppyCat opened this issue Jun 29, 2022 · 5 comments

Comments

@AppyCat
Copy link

AppyCat commented Jun 29, 2022

Hi, how is can you set the redis store as a layer or service in the main function so the connection can be used in handlers?

@jbr
Copy link
Owner

jbr commented Jun 29, 2022

Can you provide more context for this question? What framework are you using?

@AppyCat
Copy link
Author

AppyCat commented Jun 30, 2022

Yes, I am using Axum web framework.

I'm trying to use encrypted session cookies stored in Redis.

I read the docs, but don't see a way to add the Redis connection as a layer in Axum. I do see how to add the session as a session_layer.

The use case is to store a users id in an encrypted cookie with Redis as the session store.

Currently a record inserted in Redis in one handler cannot be retrieved in another handler.

Here's my setup:

use axum_sessions::{async_session::{Session, SessionStore as _}, SameSite, SessionLayer};
use async_redis_session::RedisSessionStore;

let secret = b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
let session_layer = SessionLayer::new(store.unwrap(), secret)
.with_cookie_name("test-cookie")
.with_cookie_path("/")
.with_cookie_domain("www.test-cookie.com")
.with_same_site_policy(SameSite::None)
.with_session_ttl(Some(Duration::from_secs(60 * 5)))
.with_save_unchanged(true)
.with_secure(false);

@jbr
Copy link
Owner

jbr commented Jun 30, 2022

I think this is an issue for the Axum sessions library, if I understand correctly

@AppyCat
Copy link
Author

AppyCat commented Jun 30, 2022

Thanks, I put a note out to them.

However am also getting the following errors on methods not existing in store from async-redis-session:

92 | 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:93:29
|
93 | let mut session = store.load_session("test-cookie".parse().unwrap()).await.unwrap();
| ^^^^^^^^^^^^ method not found in Result<RedisSessionStore, redis::types::RedisError>

Code is:

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 }))
}

@jbr
Copy link
Owner

jbr commented Jun 30, 2022

store is a Result that needs to be unwrapped or the error needs to be handled. Regardless, this is an issue for the axum session library.

@jbr jbr closed this as completed Jun 30, 2022
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

2 participants