Skip to content

Commit 4942591

Browse files
authored
Merge pull request #184 from refactor-group/179-renew-sessions-for-active-users
Update the Session on Reads
2 parents 1ba3569 + a9e91ac commit 4942591

File tree

7 files changed

+17
-15
lines changed

7 files changed

+17
-15
lines changed

.github/workflows/deploy_to_do.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ jobs:
9999
BACKEND_SERVICE_API_PATH=${{ vars.BACKEND_SERVICE_API_PATH }}
100100
# API version to use between frontend and backend
101101
BACKEND_API_VERSION=${{ vars.BACKEND_API_VERSION }}
102+
# Session expiry duration in seconds (default: 24 hours = 86400 seconds)
103+
BACKEND_SESSION_EXPIRY_SECONDS=${{ vars.BACKEND_SESSION_EXPIRY_SECONDS }}
102104
# Deployment environment used (development, staging, production)
103105
RUST_ENV=${{ vars.RUST_ENV }}
104106

docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ services:
7878
BACKEND_API_VERSION: ${BACKEND_API_VERSION}
7979
BACKEND_ALLOWED_ORIGINS: ${BACKEND_ALLOWED_ORIGINS}
8080
BACKEND_LOG_FILTER_LEVEL: ${BACKEND_LOG_FILTER_LEVEL}
81+
BACKEND_SESSION_EXPIRY_SECONDS: ${BACKEND_SESSION_EXPIRY_SECONDS}
8182
TIPTAP_APP_ID: ${TIPTAP_APP_ID}
8283
TIPTAP_URL: ${TIPTAP_URL}
8384
TIPTAP_AUTH_KEY: ${TIPTAP_AUTH_KEY}

entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ main() {
8787
local interface="${BACKEND_INTERFACE:-0.0.0.0}"
8888
local port="${BACKEND_PORT:-4000}"
8989
local origins="${BACKEND_ALLOWED_ORIGINS:-*}"
90+
local session_expiry="${BACKEND_SESSION_EXPIRY_SECONDS:-86400}"
9091

9192
log_info "Starting Refactor Platform API server..."
9293
log_debug "Log level: $log_level, Interface: $interface, Port: $port"
@@ -97,6 +98,7 @@ main() {
9798
-i "$interface" \
9899
-p "$port" \
99100
--allowed-origins="$origins" \
101+
--backend-session-expiry-seconds="$session_expiry" \
100102
"$@"
101103
;;
102104

service/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ pub struct Config {
142142
.map(|s| s.parse::<RustEnv>().unwrap()),
143143
)]
144144
pub runtime_env: RustEnv,
145+
146+
/// Session expiry duration in seconds (default: 24 hours = 86400 seconds)
147+
#[arg(long, env, default_value_t = 86400)]
148+
pub backend_session_expiry_seconds: u64,
145149
}
146150

147151
impl Default for Config {

web/src/extractors/authenticated_user.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ use axum::{
66
};
77
use axum_login::AuthSession;
88
use domain::users;
9-
use log::*;
10-
use tower_sessions::Session;
11-
129
pub(crate) struct AuthenticatedUser(pub users::Model);
1310

1411
#[async_trait]
@@ -26,16 +23,6 @@ where
2623
.await
2724
.map_err(|(status, msg)| (status, msg.to_string()))?;
2825

29-
// Touch the session to update activity timestamp for session renewal
30-
if let Ok(tower_session) = Session::from_request_parts(parts, state).await {
31-
if let Err(e) = tower_session.save().await {
32-
warn!("Failed to touch session for activity renewal: {e:?}");
33-
// Continue with authentication - session touch failure shouldn't block authentication
34-
} else {
35-
trace!("Session touched successfully for activity renewal");
36-
}
37-
}
38-
3926
match session.user {
4027
Some(user) => Ok(AuthenticatedUser(user)),
4128
None => Err((StatusCode::UNAUTHORIZED, "Unauthorized".to_string())),

web/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ pub async fn init_server(app_state: AppState) -> Result<()> {
5151
// Get non-secure cookies for local testing, while production automatically gets secure cookies
5252
.with_secure(app_state.config.is_production())
5353
.with_same_site(tower_sessions::cookie::SameSite::Lax) // Assists in CSRF protection
54-
.with_expiry(Expiry::OnInactivity(Duration::days(1)));
54+
.with_expiry(Expiry::OnInactivity(Duration::seconds(
55+
app_state.config.backend_session_expiry_seconds as i64,
56+
)))
57+
// Save session on every request to reset the inactivity timer
58+
// This ensures active users stay logged in
59+
.with_always_save(true);
5560

5661
// Auth service
5762
let backend = Backend::new(&app_state.database_connection);

web/src/router.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ mod organization_endpoints_tests {
493493

494494
let session_layer = SessionManagerLayer::new(session_store)
495495
.with_secure(false)
496-
.with_expiry(Expiry::OnInactivity(Duration::days(1)));
496+
.with_expiry(Expiry::OnInactivity(Duration::days(1)))
497+
.with_always_save(true);
497498

498499
// Auth service
499500
let backend = Backend::new(db);

0 commit comments

Comments
 (0)