From a3984ab34863f52f4380d65a8bb5cb6b25bcaa83 Mon Sep 17 00:00:00 2001 From: Per Stark Date: Fri, 27 Dec 2024 01:08:39 +0100 Subject: [PATCH] feat signin form --- src/bin/server.rs | 2 ++ src/server/routes/html/signin.rs | 20 ++++++++++---- templates/auth/login.html | 47 -------------------------------- templates/auth/signin_form.html | 45 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 53 deletions(-) delete mode 100644 templates/auth/login.html create mode 100644 templates/auth/signin_form.html diff --git a/src/bin/server.rs b/src/bin/server.rs index 0c95877..82c24ae 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -26,6 +26,7 @@ use zettle_db::{ html::{ index::index_handler, search_result::search_result_handler, + signin::{authenticate_user, show_signin_form}, signout::sign_out_user, signup::{process_signup_and_show_verification, show_signup_form}, }, @@ -149,6 +150,7 @@ fn html_routes( .route("/", get(index_handler)) .route("/search", get(search_result_handler)) .route("/signout", get(sign_out_user)) + .route("/signin", get(show_signin_form).post(authenticate_user)) .route( "/signup", get(show_signup_form).post(process_signup_and_show_verification), diff --git a/src/server/routes/html/signin.rs b/src/server/routes/html/signin.rs index f99a306..ef7fd0b 100644 --- a/src/server/routes/html/signin.rs +++ b/src/server/routes/html/signin.rs @@ -1,9 +1,10 @@ use axum::{ extract::State, + http::{StatusCode, Uri}, response::{IntoResponse, Redirect}, Form, }; -use axum_htmx::HxBoosted; +use axum_htmx::{HxBoosted, HxRedirect}; use axum_session_auth::AuthSession; use axum_session_surreal::SessionSurrealPool; use serde::{Deserialize, Serialize}; @@ -17,6 +18,7 @@ use super::{render_block, render_template}; pub struct SignupParams { pub email: String, pub password: String, + pub remember_me: Option, } #[derive(Serialize)] @@ -24,7 +26,7 @@ struct PageData { // name: String, } -pub async fn show_login_form( +pub async fn show_signin_form( State(state): State, auth: AuthSession, Surreal>, HxBoosted(boosted): HxBoosted, @@ -34,12 +36,12 @@ pub async fn show_login_form( } let output = match boosted { true => render_block( - "auth/signup_form.html", + "auth/signin_form.html", "body", PageData {}, state.templates, )?, - false => render_template("auth/signup_form.html", PageData {}, state.templates)?, + false => render_template("auth/signin_form.html", PageData {}, state.templates)?, }; Ok(output.into_response()) @@ -50,7 +52,13 @@ pub async fn authenticate_user( auth: AuthSession, Surreal>, Form(form): Form, ) -> Result { - let user = User::create_new(form.email, form.password, &state.surreal_db_client).await?; + let user = User::authenticate(form.email, form.password, &state.surreal_db_client).await?; auth.login_user(user.id); - Ok(()) + if form + .remember_me + .is_some_and(|string| string == "on".to_string()) + { + auth.remember_user(true); + } + Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response()) } diff --git a/templates/auth/login.html b/templates/auth/login.html deleted file mode 100644 index f508d35..0000000 --- a/templates/auth/login.html +++ /dev/null @@ -1,47 +0,0 @@ -{% block content %} -
-

Sign in to your account

- -
-
- - -
- -
- - -
- -
- - Forgot password? -
- -
- -
- -
- -
OR
- -
- Don't have an account? - Create one -
-
-
-{% endblock %} \ No newline at end of file diff --git a/templates/auth/signin_form.html b/templates/auth/signin_form.html new file mode 100644 index 0000000..d8cc02e --- /dev/null +++ b/templates/auth/signin_form.html @@ -0,0 +1,45 @@ +{% extends "head_base.html" %} +{% block body %} + +
+
+

Login to your account

+
+
+ + +
+
+ + +
+
+ +
+
+ +
+
+
+
OR
+
+ Don't have an account? + Sign up +
+
+
+{% endblock %} \ No newline at end of file