Use UserCredentials in place of username/password parameters

This commit is contained in:
Ethan Girouard 2024-04-02 01:51:15 -04:00
parent 7023e27b51
commit d6eb05514e
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146
2 changed files with 10 additions and 4 deletions

View File

@ -3,6 +3,7 @@ use leptos::leptos_dom::*;
use leptos::*;
use leptos_icons::*;
use icondata;
use crate::users::UserCredentials;
#[component]
pub fn Login() -> impl IntoView {
@ -23,7 +24,12 @@ pub fn Login() -> impl IntoView {
let password1 = password.get();
spawn_local(async move {
let login_result = login(username_or_email1, password1).await;
let user_credentials = UserCredentials {
username_or_email: username_or_email1,
password: password1
};
let login_result = login(user_credentials).await;
if let Err(err) = login_result {
// Handle the error here, e.g., log it or display to the user
log!("Error logging in: {:?}", err);

View File

@ -69,10 +69,10 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
/// Validate a user's credentials
/// Returns a Result with the user if the credentials are valid, None if not valid, or an error if there was a problem
#[cfg(feature = "ssr")]
pub async fn validate_user(username_or_email: String, password: String) -> Result<Option<User>, ServerFnError> {
pub async fn validate_user(credentials: UserCredentials) -> Result<Option<User>, ServerFnError> {
use leptos::server_fn::error::NoCustomError;
let db_user = find_user(username_or_email.clone()).await
let db_user = find_user(credentials.username_or_email.clone()).await
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
// If the user is not found, return None
@ -87,7 +87,7 @@ pub async fn validate_user(username_or_email: String, password: String) -> Resul
let password_hash = PasswordHash::new(&db_password)
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error hashing supplied password: {}", e)))?;
match Pbkdf2.verify_password(password.as_bytes(), &password_hash) {
match Pbkdf2.verify_password(credentials.password.as_bytes(), &password_hash) {
Ok(()) => {},
Err(Error::Password) => {
return Ok(None);