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

This commit is contained in:
Douze Bé 2024-10-23 22:07:57 +02:00
parent 72f046cb29
commit 09bf058c05
4 changed files with 29 additions and 8 deletions

View file

@ -4,9 +4,12 @@
<head> <head>
<title>{% block title %}{% endblock title %}</title> <title>{% block title %}{% endblock title %}</title>
<!--
<script src="https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js"></script> <script src="https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js"></script>
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio"></script> <script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio"></script>
-->
<script src="/static/htmx.min.js"></script>
<script src="/static/tailwindcss.js"></script>
{% block head %} {% block head %}
{% endblock head %} {% endblock head %}

View file

@ -7,7 +7,7 @@ Index
{% block content %} {% block content %}
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8"> <div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
<div class="sm:mx-auto sm:w-full sm:max-w-sm"> <div class="sm:mx-auto sm:w-full sm:max-w-sm">
<img class="mx-auto h-12 w-auto" src="https://nixin.distrilab.eu/logo-nixin.svg" alt="NixiN"> <img class="mx-auto h-12 w-auto" src="/static/logo-nixin.svg" alt="NixiN">
<h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900"> <h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
Index Index
</h2> </h2>

View file

@ -21,7 +21,7 @@ Login
--> -->
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8"> <div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
<div class="sm:mx-auto sm:w-full sm:max-w-sm"> <div class="sm:mx-auto sm:w-full sm:max-w-sm">
<img class="mx-auto h-12 w-auto" src="https://nixin.distrilab.eu/logo-nixin.svg" alt="NixiN"> <img class="mx-auto h-12 w-auto" src="/static/logo-nixin.svg" alt="NixiN">
<h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">Sign in to your account</h2> <h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">Sign in to your account</h2>
</div> </div>

View file

@ -8,8 +8,8 @@ use axum::{
extract::State, extract::State,
//extract::Query, //extract::Query,
response::{/*IntoResponse,*/ Redirect}, response::{/*IntoResponse,*/ Redirect},
http::StatusCode,
//Json, //Json,
http::{StatusCode, header::{self, HeaderValue, HeaderMap}},
Form}; Form};
use axum_extra::extract::cookie::{CookieJar, Cookie}; use axum_extra::extract::cookie::{CookieJar, Cookie};
@ -128,7 +128,11 @@ pub async fn do_register(
let res = AuthMailer::send_welcome(&ctx, &user).await; let res = AuthMailer::send_welcome(&ctx, &user).await;
match res { match res {
Ok(()) => { 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) => { Err(err) => {
return views::home::error(&v,&format!("failed to send welcome email: {}",err.to_string())); return views::home::error(&v,&format!("failed to send welcome email: {}",err.to_string()));
@ -179,11 +183,25 @@ pub async fn do_login(
else { else {
return Ok((jar, views::home::error(&v,"Login failed: invalid email or password"))); 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(( Ok((
// the updated jar must be returned for the changes
// to be included in the response
jar.add(Cookie::new("token", token)), 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"))); return Ok((jar, views::home::error(&v,"Login failed: you need to provide an email and a password")));