Upgrade to leptos 0.6
This commit is contained in:
43
src/auth.rs
43
src/auth.rs
@ -1,4 +1,5 @@
|
||||
use leptos::*;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use crate::models::User;
|
||||
|
||||
/// Create a new user and log them in
|
||||
@ -19,13 +20,17 @@ pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
|
||||
};
|
||||
|
||||
create_user(&new_user).await
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error creating user: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating user: {}", e)))?;
|
||||
|
||||
extract(|request: HttpRequest| async move {
|
||||
Identity::login(&request.extensions(), new_user.username.clone())
|
||||
}).await??;
|
||||
|
||||
Ok(())
|
||||
match extract::<HttpRequest>().await {
|
||||
Ok(request) => {
|
||||
match Identity::login(&request.extensions(), new_user.username.clone()) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e))),
|
||||
}
|
||||
},
|
||||
Err(e) => Err(ServerFnError::<NoCustomError>::ServerError(format!("Error getting request: {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user in
|
||||
@ -39,18 +44,22 @@ pub async fn login(username_or_email: String, password: String) -> Result<bool,
|
||||
use leptos_actix::extract;
|
||||
|
||||
let possible_user = validate_user(username_or_email, password).await
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error validating user: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error validating user: {}", e)))?;
|
||||
|
||||
let user = match possible_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(false)
|
||||
};
|
||||
|
||||
extract(|request: HttpRequest| async move {
|
||||
Identity::login(&request.extensions(), user.username.clone())
|
||||
}).await??;
|
||||
|
||||
Ok(true)
|
||||
match extract::<HttpRequest>().await {
|
||||
Ok(request) => {
|
||||
match Identity::login(&request.extensions(), user.username.clone()) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e))),
|
||||
}
|
||||
}
|
||||
Err(e) => Err(ServerFnError::<NoCustomError>::ServerError(format!("Error getting request: {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user out
|
||||
@ -60,11 +69,11 @@ pub async fn logout() -> Result<(), ServerFnError> {
|
||||
use leptos_actix::extract;
|
||||
use actix_identity::Identity;
|
||||
|
||||
extract(|user: Option<Identity>| async move {
|
||||
if let Some(user) = user {
|
||||
user.logout();
|
||||
}
|
||||
}).await?;
|
||||
match extract::<Option<Identity>>().await {
|
||||
Ok(Some(user)) => user.logout(),
|
||||
Ok(None) => {},
|
||||
Err(e) => return Err(ServerFnError::<NoCustomError>::ServerError(format!("Error getting user: {}", e))),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
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