add expiry to session

This commit is contained in:
Joe Ardent 2024-02-26 15:06:19 -08:00
parent cf0e1d3d8f
commit d985211b66
2 changed files with 8 additions and 9 deletions

View File

@ -88,7 +88,7 @@ pub async fn post_edit_signup(
} }
/// Called from Stripe with the receipt of payment. /// Called from Stripe with the receipt of payment.
pub async fn signup_success(session: Session, receipt: Option<Path<String>>) -> impl IntoResponse { pub async fn payment_success(session: Session, receipt: Option<Path<String>>) -> impl IntoResponse {
let user: User = session.get(SIGNUP_KEY).await.unwrap().unwrap_or_default(); let user: User = session.get(SIGNUP_KEY).await.unwrap().unwrap_or_default();
if user == User::default() { if user == User::default() {
return SignupErrorPage("who you?".to_string()).into_response(); return SignupErrorPage("who you?".to_string()).into_response();

View File

@ -2,16 +2,13 @@ use std::net::SocketAddr;
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tower_sessions::{MemoryStore, SessionManagerLayer}; use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
// "extern crate" is this olde-timey way to bring macros into the current live
// namespace; I'm not sure why it's needed here; we're using this to allow to do
// like such as #[derive(Error)] before a struct or enum.
#[macro_use] #[macro_use]
extern crate justerror; extern crate justerror;
mod handlers; mod handlers;
use handlers::{get_signup, post_signup, signup_success}; use handlers::{get_signup, payment_success, post_signup};
mod templates; mod templates;
@ -26,17 +23,19 @@ async fn main() {
// just for signups // just for signups
let session_store = MemoryStore::default(); let session_store = MemoryStore::default();
let session_layer = SessionManagerLayer::new(session_store).with_secure(true); let session_layer = SessionManagerLayer::new(session_store)
.with_secure(true)
.with_expiry(Expiry::OnInactivity(time::Duration::hours(16)));
// the core application, defining the routes and handlers // the core application, defining the routes and handlers
let app = Router::new() let app = Router::new()
.nest_service("/assets", assets_svc) .nest_service("/assets", assets_svc)
.route("/signup", get(get_signup).post(post_signup)) .route("/signup", get(get_signup).post(post_signup))
.route("/signup_success/:receipt", get(signup_success)) .route("/payment_success/:receipt", get(payment_success))
.layer(session_layer) .layer(session_layer)
.into_make_service(); .into_make_service();
// listing on the network // listening on the network
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();