implement flash cookies for auth change routes

This commit is contained in:
glyph 2022-03-20 12:27:00 +02:00
parent 70f7ad0dc6
commit 40c4f8aaf2
3 changed files with 51 additions and 23 deletions

View File

@ -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)

View File

@ -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<dyn std::error::Error>;
@ -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) => {

View File

@ -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<String> {
pub fn build_template(request: &Request) -> PreEscaped<String> {
// 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("<!-- CHANGE PASSWORD FORM -->"))
div class="card center" {
@ -28,9 +35,11 @@ pub fn build_template() -> PreEscaped<String> {
a class="button button-secondary center" href="/settings/admin" title="Cancel"{ "Cancel" }
}
}
(PreEscaped("<!-- FLASH MESSAGE -->"))
// 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("<!-- FLASH MESSAGE -->"))
(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(_) => (
// <cookie-name>=<cookie-value>
"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)),
}
*/