From 09bf058c05e72db24b72762c8c560be5d185e470 Mon Sep 17 00:00:00 2001 From: Fabrice Bellamy <12b@distrilab.fr> Date: Wed, 23 Oct 2024 22:07:57 +0200 Subject: [PATCH] Fix the redirect issue with htmx on the register page and actually commit the changes for static assets for htmx, tailwindcss and nixin logo that I missed in previous commit --- nixin_farm_ssr/assets/views/base.html | 5 +++- nixin_farm_ssr/assets/views/home/index.html | 2 +- nixin_farm_ssr/assets/views/home/login.html | 2 +- nixin_farm_ssr/src/controllers/home.rs | 28 +++++++++++++++++---- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/nixin_farm_ssr/assets/views/base.html b/nixin_farm_ssr/assets/views/base.html index e4d41bd..96bb9e0 100644 --- a/nixin_farm_ssr/assets/views/base.html +++ b/nixin_farm_ssr/assets/views/base.html @@ -4,9 +4,12 @@ {% block title %}{% endblock title %} - + + + {% block head %} {% endblock head %} diff --git a/nixin_farm_ssr/assets/views/home/index.html b/nixin_farm_ssr/assets/views/home/index.html index 0b2775a..878a733 100644 --- a/nixin_farm_ssr/assets/views/home/index.html +++ b/nixin_farm_ssr/assets/views/home/index.html @@ -7,7 +7,7 @@ Index {% block content %}
- NixiN + NixiN

Index

diff --git a/nixin_farm_ssr/assets/views/home/login.html b/nixin_farm_ssr/assets/views/home/login.html index d84684c..2b43a9d 100644 --- a/nixin_farm_ssr/assets/views/home/login.html +++ b/nixin_farm_ssr/assets/views/home/login.html @@ -21,7 +21,7 @@ Login -->
- NixiN + NixiN

Sign in to your account

diff --git a/nixin_farm_ssr/src/controllers/home.rs b/nixin_farm_ssr/src/controllers/home.rs index 89697a6..d0b3e06 100644 --- a/nixin_farm_ssr/src/controllers/home.rs +++ b/nixin_farm_ssr/src/controllers/home.rs @@ -8,8 +8,8 @@ use axum::{ extract::State, //extract::Query, response::{/*IntoResponse,*/ Redirect}, - http::StatusCode, //Json, + http::{StatusCode, header::{self, HeaderValue, HeaderMap}}, Form}; use axum_extra::extract::cookie::{CookieJar, Cookie}; @@ -128,7 +128,11 @@ pub async fn do_register( let res = AuthMailer::send_welcome(&ctx, &user).await; match res { Ok(()) => { - return format::redirect("/login"); + let mut headers = HeaderMap::new(); + //ToDo: modifiy below to avoid using ugly unwrap + headers.insert("HX-Location", "/".parse().unwrap()); + + return Ok(headers.into_response()); } Err(err) => { return views::home::error(&v,&format!("failed to send welcome email: {}",err.to_string())); @@ -179,11 +183,25 @@ pub async fn do_login( else { return Ok((jar, views::home::error(&v,"Login failed: invalid email or password"))); }; + let mut headers = HeaderMap::new(); + //ToDo: modifiy below to avoid using ugly unwrap + headers.insert(header::AUTHORIZATION, HeaderValue::from_str(&token).unwrap()); + headers.insert("HX-Location", "/".parse().unwrap()); + + // We do not really need to return the index view in the response body + // because the HX-Location header will trigger htmx to fetch it from + // the client. + // We are doing it here only to test if it allows degrading gracefully + // on java script disabled clients. + // See do_register() for an implementation of htmx redirect with + // an empty body + let index_view = views::home::index(&v,&user).unwrap(); + // Also we are returning the JWT token both in a header and in a cookie + // This is also done for test purposes only. + // Only one of these should be done, the one used in the auth configuration. Ok(( - // the updated jar must be returned for the changes - // to be included in the response jar.add(Cookie::new("token", token)), - Ok(Redirect::to("/").into_response()),)) + Ok((headers, index_view).into_response()),)) } _ => { return Ok((jar, views::home::error(&v,"Login failed: you need to provide an email and a password")));