feat signin form

This commit is contained in:
Per Stark
2024-12-27 01:08:39 +01:00
parent 61e81ca894
commit a3984ab348
4 changed files with 61 additions and 53 deletions

View File

@@ -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())
}