Lines
100 %
Functions
30.28 %
Branches
use askama::Template;
use axum::response::{Html, IntoResponse, Response};
use axum_extra::extract::CookieJar;
use crate::response::WebError;
pub mod account;
pub mod commodity;
pub mod config;
pub mod forms;
pub mod report;
pub mod script;
pub mod tag;
pub mod template;
pub mod transaction;
/// Whether the cookie `name` holds a token of `kind` that verifies. A directory
/// outage counts as not verified: this only decides which navigation the index
/// shows.
async fn holds_valid_token(
cookie_jar: &CookieJar,
name: &str,
kind: crate::token::TokenType,
) -> bool {
match cookie_jar.get(name) {
Some(cookie) => crate::auth_keys::verify(cookie.value(), kind)
.await
.is_ok_and(|details| details.is_some()),
None => false,
}
/// The access cookie lives minutes, the refresh cookie days, and a request
/// that carries only the latter is refreshed by the middleware.
pub async fn index(cookie_jar: CookieJar) -> impl IntoResponse {
let is_logged_in =
holds_valid_token(&cookie_jar, "access_token", crate::token::TokenType::Access).await
|| holds_valid_token(
&cookie_jar,
"refresh_token",
crate::token::TokenType::Refresh,
)
.await;
let template = IndexTemplate {
is_logged_in,
scripting_enabled: true,
};
HtmlTemplate(template)
#[derive(Template)]
#[template(path = "pages/index.html")]
struct IndexTemplate {
is_logged_in: bool,
scripting_enabled: bool,
pub async fn register() -> impl IntoResponse {
let template = RegisterTemplate {};
#[template(path = "pages/register.html")]
struct RegisterTemplate;
pub async fn login() -> impl IntoResponse {
HtmlTemplate(crate::login::LoginTemplate::default())
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
struct HtmlTemplate<T>(T);
/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => WebError::internal("Failed to render template", err).into_response(),
#[cfg(test)]
#[path = "pages_tests.rs"]
mod tests;