GithubHelp home page GithubHelp logo

Comments (5)

Katze864 avatar Katze864 commented on May 31, 2024 1

This seems a little long for a minimal example, feedback on how to do it better would be welcome.
On reloading the page http://127.0.0.1:3000/ after more than 2 seconds the warning WARN [tower_sessions_core::session] possibly suspicious activity: record not found in store is shown, which indicates that the session has expired and has been deleted.

use std::net::SocketAddr;

use axum::{response::IntoResponse, routing::get, Router, middleware, middleware::Next, http::{Request, Response}, body::Body as AxumBody};
use time::{OffsetDateTime, Duration};
use tower_sessions::{Expiry, Session, SessionManagerLayer};
use sqlx::sqlite::SqlitePoolOptions;
use tower_sessions_sqlx_store::SqliteStore;


async fn change_expiry(
    session: Session, 
    request: Request<AxumBody>,
    next: Next,
) -> Response<AxumBody> {
    
    //modify the session, so it gets stored to the db
    session.insert("last_accessed", OffsetDateTime::now_utc()).await.unwrap();

    // change the session expiry
    let expired_at = OffsetDateTime::now_utc().saturating_add(Duration::days(60));
    session.set_expiry(Some(Expiry::AtDateTime(expired_at)));

    let response = next.run(request).await;
    response
}

#[tokio::main]
async fn main() {
    simple_logger::init_with_level(log::Level::Warn)
        .expect("couldn't initialize logging");

    let pool = SqlitePoolOptions::new()
        .connect("sqlite:database.db")
        .await
        .expect("Could not make pool.");

    let session_store = SqliteStore::new(pool.clone());
    let session_layer = SessionManagerLayer::new(session_store)
        .with_secure(false)
        .with_expiry(Expiry::OnInactivity(Duration::seconds(2)));

    let app = Router::new().route("/", get(handler))
        .layer(middleware::from_fn(change_expiry))
        .layer(session_layer);

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();
}

async fn handler() -> impl IntoResponse {
    format!("HI")
}

from tower-sessions.

maxcountryman avatar maxcountryman commented on May 31, 2024 1

This should be addressed in the 0.11.1 release.

from tower-sessions.

maxcountryman avatar maxcountryman commented on May 31, 2024

Please provide a minimal example that reproduces what you're seeing.

from tower-sessions.

maxcountryman avatar maxcountryman commented on May 31, 2024

Thank you, this is very helpful.

I've put together a test case and am working on a patch.

from tower-sessions.

Katze864 avatar Katze864 commented on May 31, 2024

Thanks

from tower-sessions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.