feat: minimal, naive login

master
Araozu 2024-08-10 18:33:46 -05:00
parent b1feb86204
commit 19289e8b36
4 changed files with 41 additions and 12 deletions

View File

@ -1,7 +1,8 @@
use std::time::Duration;
use maud::Markup; use maud::Markup;
use rocket::{form::Form, http::Status, response::Redirect, tokio::time::sleep}; use rocket::{form::Form, http::{CookieJar, Status}, response::Redirect};
use utils::RS_SESSION_ID;
use crate::db;
mod utils; mod utils;
@ -12,6 +13,7 @@ pub fn homepage() -> Markup {
#[get("/new")] #[get("/new")]
pub fn new_definition(_user: utils::User) -> (Status, Markup) { pub fn new_definition(_user: utils::User) -> (Status, Markup) {
log::info!("reached /new");
todo!() todo!()
} }
@ -27,20 +29,39 @@ pub struct LoginData {
pub login_password: String, pub login_password: String,
} }
#[get("/login")] #[get("/login")]
pub fn login_page() -> Markup { pub fn login_page() -> Markup {
crate::view::login::login() crate::view::login::login()
} }
#[post("/login", data = "<data>")] #[post("/login", data = "<data>")]
pub async fn login(data: Form<LoginData>) -> (Status, String) { pub async fn login(data: Form<LoginData>, cookies: &CookieJar<'_>) -> (Status, String) {
println!("begin request: {}", data.login_email); println!("begin request: {}", data.login_email);
let db = match db().await {
Ok(handle) => handle,
Err(reason) => return (Status::InternalServerError, reason),
};
// Simulate trip to db let result = sqlx::query!(
sleep(Duration::new(5, 0)).await; "select * from person where person_email = $1 and person_password = $2",
println!("end request: {}", data.login_password); &data.login_email,
(Status::Ok, ":D".into()) &data.login_password,
)
.fetch_all(db)
.await;
let re = match result {
Ok(r) => r,
Err(reason) => return (Status::InternalServerError, format!("{:?}", reason)),
};
if !re.is_empty() {
// TODO: generate a session id and assign
cookies.add_private((RS_SESSION_ID, "session-id"));
(Status::Ok, "<div _='init go to url /new'></div>".into())
} else {
(Status::Unauthorized, "Correo o contraseña invalida".into())
}
} }

View File

@ -4,7 +4,7 @@ use rocket::{
}; };
/// Name of the header that stores the session ID of an user /// Name of the header that stores the session ID of an user
const RS_SESSION_ID: &str = "x-rs-session-id"; pub const RS_SESSION_ID: &str = "x-rs-session-id";
pub struct User {} pub struct User {}

View File

@ -8,12 +8,15 @@ pub fn login() -> Markup {
"Jerguero" "Jerguero"
} }
div class="flex items-center h-screen w-full" { div class="flex items-center h-screen w-full" {
div class="container mx-auto" { div class="container mx-auto" "hx-ext"="response-targets" {
p class="my-4 p-2 rounded bg-c-bg-2 text-c-on-bg" { p class="my-4 p-2 rounded bg-c-bg-2 text-c-on-bg" {
"¡Iniciá sesión para comenzar a crear definiciones!" "¡Iniciá sesión para comenzar a crear definiciones!"
} }
form class="my-4 py-4 px-2 rounded bg-c-bg-2 text-c-on-bg" form class="my-4 py-4 px-2 rounded bg-c-bg-2 text-c-on-bg"
"hx-post"="/login" "hx-post"="/login"
"hx-swap"="innerHTML"
"hx-target"="#login-result"
"hx-target-error"="#login-result-error"
{ {
div class="py-2" { div class="py-2" {
label class="text-sm opacity-85" for="login-email" {"Correo electronico:"} label class="text-sm opacity-85" for="login-email" {"Correo electronico:"}
@ -49,7 +52,11 @@ pub fn login() -> Markup {
"Iniciar sesión" "Iniciar sesión"
} }
} }
div id="login-result" class="text-center pt-2" {}
div id="login-result-error" class="text-center pt-2 text-red-400" {}
} }
} }
} }
}) })

View File

@ -24,6 +24,7 @@ pub fn skeleton(body: Markup) -> Markup {
// htmx // htmx
script src="https://unpkg.com/htmx.org@2.0.0" {} script src="https://unpkg.com/htmx.org@2.0.0" {}
script src="https://unpkg.com/htmx-ext-loading-states@2.0.0/loading-states.js" {} script src="https://unpkg.com/htmx-ext-loading-states@2.0.0/loading-states.js" {}
script src="https://unpkg.com/htmx-ext-response-targets@2.0.0/response-targets.js" {}
// hyperscript // hyperscript
script src="https://unpkg.com/hyperscript.org@0.9.12" defer {} script src="https://unpkg.com/hyperscript.org@0.9.12" defer {}
} }