mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-20 16:44:12 +01:00
feat signin form
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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<String>,
|
||||
}
|
||||
|
||||
#[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<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
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<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user