From 40c4f8aaf2b65bf774857fcdc9906145e89dcf73 Mon Sep 17 00:00:00 2001 From: glyph Date: Sun, 20 Mar 2022 12:27:00 +0200 Subject: [PATCH] implement flash cookies for auth change routes --- peach-web/rouille_refactor | 16 ++++++ peach-web/src/main.rs | 7 ++- peach-web/src/routes/authentication/change.rs | 51 +++++++++++-------- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/peach-web/rouille_refactor b/peach-web/rouille_refactor index 06e59ba..f8ad8d7 100644 --- a/peach-web/rouille_refactor +++ b/peach-web/rouille_refactor @@ -10,6 +10,22 @@ we do not need to be super fast or feature-rich. - use the one-file-per-route patten +[ rouille-specific ] + + - logging + - https://docs.rs/rouille/latest/rouille/fn.log_custom.html + x flash message + - https://docs.rs/rouille/latest/rouille/input/fn.cookies.html + - https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#creating_cookies + - https://docs.rs/rouille/latest/rouille/struct.Response.html#method.with_additional_header + - https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_the_lifetime_of_a_cookie + - file upload + - https://docs.rs/rouille/latest/rouille/input/post/index.html#handling-file-uploads + - auth + - https://github.com/tomaka/rouille/blob/master/examples/login-session.rs + - https://docs.rs/rouille/latest/rouille/struct.Response.html#method.basic_http_auth_login_required + + [ tasks ] - write the settings route(s) diff --git a/peach-web/src/main.rs b/peach-web/src/main.rs index b30719e..baed99d 100644 --- a/peach-web/src/main.rs +++ b/peach-web/src/main.rs @@ -34,7 +34,7 @@ use rouille::{router, Response}; // crate-local dependencies use config::Config; -use utils::Theme; +use utils::{flash::FlashResponse, theme::Theme}; pub type BoxError = Box; @@ -129,7 +129,10 @@ fn main() { }, (GET) (/auth/change) => { - Response::html(routes::authentication::change::build_template()) + // build the html template + Response::html(routes::authentication::change::build_template(request)) + // reset the flash msg cookies in the response object + .reset_flash() }, (POST) (/auth/change) => { diff --git a/peach-web/src/routes/authentication/change.rs b/peach-web/src/routes/authentication/change.rs index 2fdd172..f85e9e9 100644 --- a/peach-web/src/routes/authentication/change.rs +++ b/peach-web/src/routes/authentication/change.rs @@ -1,14 +1,21 @@ use log::info; use maud::{html, PreEscaped}; use peach_lib::password_utils; -use rouille::{post_input, try_or_400, Request, Response}; +use rouille::{input, post_input, try_or_400, Request, Response}; -use crate::{error::PeachWebError, templates}; +use crate::{ + error::PeachWebError, + templates, + utils::flash::{FlashRequest, FlashResponse}, +}; // HELPER AND ROUTES FOR /auth/change (GET and POST) /// Password change form template builder. -pub fn build_template() -> PreEscaped { +pub fn build_template(request: &Request) -> PreEscaped { + // check for flash cookies; will be (None, None) if no flash cookies are found + let (flash_name, flash_msg) = request.retrieve_flash(); + let form_template = html! { (PreEscaped("")) div class="card center" { @@ -28,9 +35,11 @@ pub fn build_template() -> PreEscaped { a class="button button-secondary center" href="/settings/admin" title="Cancel"{ "Cancel" } } } - (PreEscaped("")) - // TODO: render flash message - //{% include "snippets/flash_message" %} + // render flash message if cookies were found in the request + @if let (Some(name), Some(msg)) = (flash_name, flash_msg) { + (PreEscaped("")) + (templates::flash::build_template(name, msg)) + } } }; @@ -79,23 +88,23 @@ pub fn handle_form(request: &Request) -> Response { })); // save submitted admin id to file - let _result = save_password( + // match on the result and set flash name and msg accordingly + let (flash_name, flash_msg) = match save_password( &data.current_password, &data.new_password1, &data.new_password2, - ); + ) { + Ok(_) => ( + // = + "flash_name=success".to_string(), + "flash_msg=New password has been saved".to_string(), + ), + Err(err) => ( + "flash_name=error".to_string(), + format!("flash_msg=Failed to save new password: {}", err), + ), + }; - // TODO: match on result and define flash message accordingly - // then send the redirect response - - // redirect to the configure admin page - // TODO: add flash message - Response::redirect_303("/auth/change") + // set the flash cookie headers and redirect to the configure admin page + Response::redirect_303("/auth/change").add_flash(flash_name, flash_msg) } - -/* - match result { - Ok(_) => Flash::success(Redirect::to(url), "Added SSB administrator"), - Err(e) => Flash::error(Redirect::to(url), format!("Failed to add new admin: {}", e)), - } -*/