AscendingCreations / AxumSession

Axum Session Management Libraries that use Sqlx
MIT License
142 stars 29 forks source link

Session not saved sometimes #19

Closed bbigras closed 2 years ago

bbigras commented 2 years ago

I'm using set() with a redirect right after and in some conditions, the change is not saved to the database.

session.set(SESSION_KEY_USER, user_id).await;
return Ok(Redirect::to("/"));

Any ideas?

bbigras commented 2 years ago

Note that the user is also not set in the session after the redirect.

genusistimelord commented 2 years ago

what version of the library are you currently using and what options do you have set for the session? Also what database are you using Postgresql, mysql, sqlite? @bbigras

bbigras commented 2 years ago

PostgreSQL

let session_config = AxumSessionConfig::default()
  .with_table_name("sessions");
axum_database_sessions = { version = "3.0.0", features = [ "postgres", "rustls"] }
axum_sessions_auth = { version = "2.0", features = [ "postgres", "rustls" ] }

I think I have a test case: https://github.com/bbigras/test_axum_database_sessions

steps:

I hope I didn't do anything stupid.

genusistimelord commented 2 years ago

I am getting

listening on 127.0.0.1:3000
[src\main.rs:40] session.get("count").await = Some(
    0,
)
[src\main.rs:40] session.get("count").await = Some(
    1,
)
[src\main.rs:40] session.get("count").await = Some(
    2,
)
[src\main.rs:40] session.get("count").await = Some(
    3,
)
[src\main.rs:40] session.get("count").await = Some(
    4,
)
[src\main.rs:40] session.get("count").await = Some(
    5,
)
[src\main.rs:40] session.get("count").await = Some(
    6,
)
[src\main.rs:40] session.get("count").await = Some(
    7,
)
[src\main.rs:40] session.get("count").await = Some(
    -10,
)

Please check to make sure your browser is not clearing the cookie's and to make sure your session_id cookie is staying persistent across. Also just to be sure i set the count to be a i32 since usize cant be negative.

After running it with a i32 count and then trying to read it as a usize count it does return None. So make sure the types between are correct. Please try changing it to a i32 in greet. If that works and the counting works then please test some more things. @bbigras

bbigras commented 2 years ago

I updated my test case to use a String and it's fine. I'll test my real code and get back to you.

Another weird thing is when I use session.destroy() to logout.

If you want to test, git pull my test case and try:

genusistimelord commented 2 years ago

Congrats! you found a bug =3. So yeah the issue with that is destroy is not getting set back to false after resetting the internal hash table. Thank you for that find. Also we do have a discord channel to if you ever want to talk more in depth and get quicker help on things. Ill release a patch to fix destroy.

Just released a patch 3.0.1 should fix the session issue. I am publishing it now. you can give it a try once it is up on crates.io or try the master repository. Let me know if you do find anything else. @bbigras

genusistimelord commented 2 years ago

I missed a destroy = false....

genusistimelord commented 2 years ago

this has been fixed in 3.0.2