Use UserCredentials in place of username/password parameters
This commit is contained in:
parent
7023e27b51
commit
d6eb05514e
@ -3,6 +3,7 @@ use leptos::leptos_dom::*;
|
|||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_icons::*;
|
use leptos_icons::*;
|
||||||
use icondata;
|
use icondata;
|
||||||
|
use crate::users::UserCredentials;
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Login() -> impl IntoView {
|
pub fn Login() -> impl IntoView {
|
||||||
@ -23,7 +24,12 @@ pub fn Login() -> impl IntoView {
|
|||||||
let password1 = password.get();
|
let password1 = password.get();
|
||||||
|
|
||||||
spawn_local(async move {
|
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 {
|
if let Err(err) = login_result {
|
||||||
// Handle the error here, e.g., log it or display to the user
|
// Handle the error here, e.g., log it or display to the user
|
||||||
log!("Error logging in: {:?}", err);
|
log!("Error logging in: {:?}", err);
|
||||||
|
@ -69,10 +69,10 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
|||||||
/// Validate a user's credentials
|
/// 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
|
/// 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")]
|
#[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;
|
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)))?;
|
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||||
|
|
||||||
// If the user is not found, return None
|
// 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)
|
let password_hash = PasswordHash::new(&db_password)
|
||||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error hashing supplied password: {}", e)))?;
|
.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(()) => {},
|
Ok(()) => {},
|
||||||
Err(Error::Password) => {
|
Err(Error::Password) => {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user