From 19289e8b36a0491bf70949fa8626aa0ef586c978 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 10 Aug 2024 18:33:46 -0500 Subject: [PATCH] feat: minimal, naive login --- src/controller/mod.rs | 41 +++++++++++++++++++++++++++++++---------- src/controller/utils.rs | 2 +- src/view/login.rs | 9 ++++++++- src/view/mod.rs | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 690a325..8155cf0 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -1,7 +1,8 @@ -use std::time::Duration; - 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; @@ -12,6 +13,7 @@ pub fn homepage() -> Markup { #[get("/new")] pub fn new_definition(_user: utils::User) -> (Status, Markup) { + log::info!("reached /new"); todo!() } @@ -27,20 +29,39 @@ pub struct LoginData { pub login_password: String, } - #[get("/login")] pub fn login_page() -> Markup { crate::view::login::login() } #[post("/login", data = "")] -pub async fn login(data: Form) -> (Status, String) { +pub async fn login(data: Form, cookies: &CookieJar<'_>) -> (Status, String) { println!("begin request: {}", data.login_email); - + let db = match db().await { + Ok(handle) => handle, + Err(reason) => return (Status::InternalServerError, reason), + }; - // Simulate trip to db - sleep(Duration::new(5, 0)).await; - println!("end request: {}", data.login_password); - (Status::Ok, ":D".into()) + let result = sqlx::query!( + "select * from person where person_email = $1 and person_password = $2", + &data.login_email, + &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, "
".into()) + } else { + (Status::Unauthorized, "Correo o contraseña invalida".into()) + } } diff --git a/src/controller/utils.rs b/src/controller/utils.rs index 614661f..504e42e 100644 --- a/src/controller/utils.rs +++ b/src/controller/utils.rs @@ -4,7 +4,7 @@ use rocket::{ }; /// 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 {} diff --git a/src/view/login.rs b/src/view/login.rs index ec0f38a..db03cf0 100644 --- a/src/view/login.rs +++ b/src/view/login.rs @@ -8,12 +8,15 @@ pub fn login() -> Markup { "Jerguero" } 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" { "¡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" "hx-post"="/login" + "hx-swap"="innerHTML" + "hx-target"="#login-result" + "hx-target-error"="#login-result-error" { div class="py-2" { label class="text-sm opacity-85" for="login-email" {"Correo electronico:"} @@ -49,7 +52,11 @@ pub fn login() -> Markup { "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" {} } + } } }) diff --git a/src/view/mod.rs b/src/view/mod.rs index 5eb7a01..247d63b 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -24,6 +24,7 @@ pub fn skeleton(body: Markup) -> Markup { // htmx 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-response-targets@2.0.0/response-targets.js" {} // hyperscript script src="https://unpkg.com/hyperscript.org@0.9.12" defer {} }