Upgrade to leptos 0.6
This commit is contained in:
17
src/users.rs
17
src/users.rs
@ -14,6 +14,7 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
|
||||
use leptos::*;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use crate::models::User;
|
||||
|
||||
/// Get a user from the database by username or email
|
||||
@ -26,7 +27,7 @@ pub async fn find_user(username_or_email: String) -> Result<Option<User>, Server
|
||||
let db_con = &mut get_db_conn();
|
||||
let user = users.filter(username.eq(username_or_email.clone())).or_filter(email.eq(username_or_email))
|
||||
.first::<User>(db_con).optional()
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
@ -38,11 +39,11 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
use crate::schema::users::dsl::*;
|
||||
|
||||
let new_password = new_user.password.clone()
|
||||
.ok_or(ServerFnError::ServerError(format!("No password provided for user {}", new_user.username)))?;
|
||||
.ok_or(ServerFnError::<NoCustomError>::ServerError(format!("No password provided for user {}", new_user.username)))?;
|
||||
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
let password_hash = Pbkdf2.hash_password(new_password.as_bytes(), &salt)
|
||||
.map_err(|_| ServerFnError::ServerError("Error hashing password".to_string()))?.to_string();
|
||||
.map_err(|_| ServerFnError::<NoCustomError>::ServerError("Error hashing password".to_string()))?.to_string();
|
||||
|
||||
let new_user = User {
|
||||
password: Some(password_hash),
|
||||
@ -52,7 +53,7 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
let db_con = &mut get_db_conn();
|
||||
|
||||
diesel::insert_into(users).values(&new_user).execute(db_con)
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error creating user: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating user: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -62,7 +63,7 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn validate_user(username_or_email: String, password: String) -> Result<Option<User>, ServerFnError> {
|
||||
let db_user = find_user(username_or_email.clone()).await
|
||||
.map_err(|e| ServerFnError::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
|
||||
let db_user = match db_user {
|
||||
@ -71,10 +72,10 @@ pub async fn validate_user(username_or_email: String, password: String) -> Resul
|
||||
};
|
||||
|
||||
let db_password = db_user.password.clone()
|
||||
.ok_or(ServerFnError::ServerError(format!("No password found for user {}", db_user.username)))?;
|
||||
.ok_or(ServerFnError::<NoCustomError>::ServerError(format!("No password found for user {}", db_user.username)))?;
|
||||
|
||||
let password_hash = PasswordHash::new(&db_password)
|
||||
.map_err(|e| ServerFnError::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) {
|
||||
Ok(()) => {},
|
||||
@ -82,7 +83,7 @@ pub async fn validate_user(username_or_email: String, password: String) -> Resul
|
||||
return Ok(None);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(ServerFnError::ServerError(format!("Error verifying password: {}", e)));
|
||||
return Err(ServerFnError::<NoCustomError>::ServerError(format!("Error verifying password: {}", e)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user