Compare commits

..

2 commits

Author SHA1 Message Date
Joe Ardent
d985211b66 add expiry to session 2024-02-26 15:06:19 -08:00
Joe Ardent
cf0e1d3d8f add time to deps 2024-02-26 15:06:01 -08:00
4 changed files with 10 additions and 9 deletions

1
Cargo.lock generated
View file

@ -676,6 +676,7 @@ dependencies = [
"justerror",
"serde",
"thiserror",
"time",
"tokio",
"tower-http",
"tower-sessions",

View file

@ -10,6 +10,7 @@ axum = { version = "0.7", default-features = false, features = ["tokio", "http1"
justerror = { version = "1" }
serde = { version = "1", default-features = false, features = ["derive"] }
thiserror = { version = "1" }
time = { version = "0.3.34", default-features = false }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
tower-http = { version = "0.5.2", default-features = false, features = ["fs"] }
tower-sessions = { version = "0.10", default-features = false, features = ["axum-core", "memory-store"] }

View file

@ -88,7 +88,7 @@ pub async fn post_edit_signup(
}
/// 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();
if user == User::default() {
return SignupErrorPage("who you?".to_string()).into_response();

View file

@ -2,16 +2,13 @@ use std::net::SocketAddr;
use axum::{routing::get, Router};
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]
extern crate justerror;
mod handlers;
use handlers::{get_signup, post_signup, signup_success};
use handlers::{get_signup, payment_success, post_signup};
mod templates;
@ -26,17 +23,19 @@ async fn main() {
// just for signups
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
let app = Router::new()
.nest_service("/assets", assets_svc)
.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)
.into_make_service();
// listing on the network
// listening on the network
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();