fix: move ownership into auth fn

This commit is contained in:
Per Stark
2025-03-21 08:01:04 +01:00
parent b4cf020f36
commit 42e63600a1
4 changed files with 8 additions and 11 deletions

View File

@@ -122,8 +122,8 @@ impl User {
} }
pub async fn authenticate( pub async fn authenticate(
email: String, email: &str,
password: String, password: &str,
db: &SurrealDbClient, db: &SurrealDbClient,
) -> Result<Self, AppError> { ) -> Result<Self, AppError> {
let user: Option<User> = db let user: Option<User> = db
@@ -133,8 +133,8 @@ impl User {
WHERE email = $email WHERE email = $email
AND crypto::argon2::compare(password, $password)", AND crypto::argon2::compare(password, $password)",
) )
.bind(("email", email)) .bind(("email", email.to_owned()))
.bind(("password", password)) .bind(("password", password.to_owned()))
.await? .await?
.take(0)?; .take(0)?;
user.ok_or(AppError::Auth("User failed to authenticate".into())) user.ok_or(AppError::Auth("User failed to authenticate".into()))

View File

@@ -5,12 +5,7 @@ use async_openai::{
CreateChatCompletionRequest, CreateChatCompletionRequestArgs, CreateChatCompletionResponse, CreateChatCompletionRequest, CreateChatCompletionRequestArgs, CreateChatCompletionResponse,
ResponseFormat, ResponseFormatJsonSchema, ResponseFormat, ResponseFormatJsonSchema,
}, },
MessageFiles,
}; };
use serde::Deserialize;
use serde_json::{json, Value};
use tracing::debug;
use common::{ use common::{
error::AppError, error::AppError,
storage::{ storage::{
@@ -21,6 +16,8 @@ use common::{
}, },
}, },
}; };
use serde::Deserialize;
use serde_json::{json, Value};
use crate::retrieve_entities; use crate::retrieve_entities;

View File

@@ -127,7 +127,7 @@ pub async fn change_password(
Form(form): Form<NewPasswordForm>, Form(form): Form<NewPasswordForm>,
) -> Result<impl IntoResponse, HtmlError> { ) -> Result<impl IntoResponse, HtmlError> {
// Authenticate to make sure the password matches // Authenticate to make sure the password matches
let authenticated_user = User::authenticate(user.email, form.old_password, &state.db).await?; let authenticated_user = User::authenticate(&user.email, &form.old_password, &state.db).await?;
User::patch_password(&authenticated_user.email, &form.new_password, &state.db).await?; User::patch_password(&authenticated_user.email, &form.new_password, &state.db).await?;

View File

@@ -42,7 +42,7 @@ pub async fn authenticate_user(
auth: AuthSessionType, auth: AuthSessionType,
Form(form): Form<SignupParams>, Form(form): Form<SignupParams>,
) -> Result<impl IntoResponse, HtmlError> { ) -> Result<impl IntoResponse, HtmlError> {
let user = match User::authenticate(form.email, form.password, &state.db).await { let user = match User::authenticate(&form.email, &form.password, &state.db).await {
Ok(user) => user, Ok(user) => user,
Err(_) => { Err(_) => {
return Ok(Html("<p>Incorrect email or password </p>").into_response()); return Ok(Html("<p>Incorrect email or password </p>").into_response());