Skip to content

Commit

Permalink
Set Max-Age on cookies to persist across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
kellpossible committed Dec 4, 2023
1 parent b1da388 commit 6654fa5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/disclaimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
templates::{render, TemplatesWithContext},
};
use axum::{
body::Body,
middleware::Next,
response::{IntoResponse, Redirect, Response},
};
Expand All @@ -21,6 +20,9 @@ const DISCLAIMER_COOKIE_NAME: &str = "disclaimer";
/// accepted the pevious version.
const DISCLAIMER_VERSION: u32 = 1;

/// The Max-Age property for the cookie (in seconds).
const DISCLAIMER_COOKIE_MAX_AGE_SECONDS: u64 = 365 * 24 * 60 * 60;

/// Handler to accept the disclaimer by setting a cookie [`DISCLAIMER_COOKIE_NAME`].
pub async fn handler(headers: HeaderMap) -> axum::response::Result<impl IntoResponse> {
let referer_str = headers
Expand All @@ -32,7 +34,7 @@ pub async fn handler(headers: HeaderMap) -> axum::response::Result<impl IntoResp
.map_err(map_eyre_error)?;

let mut response = Redirect::to(referer_str).into_response();
let value = HeaderValue::from_str(&format!("{DISCLAIMER_COOKIE_NAME}=v{DISCLAIMER_VERSION}"))
let value = HeaderValue::from_str(&format!("{DISCLAIMER_COOKIE_NAME}=v{DISCLAIMER_VERSION}; Max-Age={DISCLAIMER_COOKIE_MAX_AGE_SECONDS}"))
.map_err(map_std_error)?;
response.headers_mut().insert(SET_COOKIE, value);
Ok(response)
Expand Down
8 changes: 6 additions & 2 deletions src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub struct Query {

/// The name of the cookie used to store the user selected language.
const LANG_COOKIE_NAME: &str = "lang";
/// The Max-Age property for the cookie (in seconds).
const LANG_COOKIE_MAX_AGE_SECONDS: u64 = 365 * 24 * 60 * 60;

/// Handler to select a language by setting the [`LANG_COOKIE_NAME`] cookie.
pub async fn handler(
Expand All @@ -97,8 +99,10 @@ pub async fn handler(
.map_err(map_eyre_error)?;
let mut response = Redirect::to(referer_str).into_response();
let lang = query.lang;
let value =
HeaderValue::from_str(&format!("{LANG_COOKIE_NAME}={lang}")).map_err(map_std_error)?;
let value = HeaderValue::from_str(&format!(
"{LANG_COOKIE_NAME}={lang}; Max-Age={LANG_COOKIE_MAX_AGE_SECONDS}"
))
.map_err(map_std_error)?;
response.headers_mut().insert(SET_COOKIE, value);
Ok(response)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async fn main() -> eyre::Result<()> {
.route("/", get(index::handler))
.route("/forecasts/:file_name", get(forecasts::handler))
.nest("/observations", observations::router())
.layer(middleware::from_fn(disclaimer::middleware))
.layer(middleware::from_fn(disclaimer::middleware)),
)
.nest("/diagrams", diagrams::router())
.nest("/forecast-areas", forecast_areas::router())
Expand Down

0 comments on commit 6654fa5

Please sign in to comment.