Switch to using axum_login in authentication server functions
This commit is contained in:
parent
8bbb37c1e1
commit
e7d0769066
84
src/auth.rs
84
src/auth.rs
@ -1,6 +1,18 @@
|
||||
use leptos::*;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use leptos_axum::extract;
|
||||
use axum_login::AuthSession;
|
||||
use crate::auth_backend::AuthBackend;
|
||||
}
|
||||
}
|
||||
|
||||
use crate::models::User;
|
||||
use crate::users::UserCredentials;
|
||||
|
||||
/// Create a new user and log them in
|
||||
/// Takes in a NewUser struct, with the password in plaintext
|
||||
@ -9,8 +21,6 @@ use crate::models::User;
|
||||
pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
|
||||
use crate::users::create_user;
|
||||
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
// Ensure the user has no id
|
||||
let new_user = User {
|
||||
id: None,
|
||||
@ -20,59 +30,59 @@ pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
|
||||
create_user(&new_user).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating user: {}", e)))?;
|
||||
|
||||
/*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))),
|
||||
}*/
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
let credentials = UserCredentials {
|
||||
username_or_email: new_user.username.clone(),
|
||||
password: new_user.password.clone().unwrap()
|
||||
};
|
||||
|
||||
match auth_session.authenticate(credentials).await {
|
||||
Ok(Some(user)) => {
|
||||
auth_session.login(&user).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e)))
|
||||
},
|
||||
Ok(None) => {
|
||||
Err(ServerFnError::<NoCustomError>::ServerError("Error authenticating user: User not found".to_string()))
|
||||
},
|
||||
Err(e) => {
|
||||
Err(ServerFnError::<NoCustomError>::ServerError(format!("Error authenticating user: {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user in
|
||||
/// Takes in a username or email and a password in plaintext
|
||||
/// Returns a Result with a boolean indicating if the login was successful
|
||||
#[server(endpoint = "login")]
|
||||
pub async fn login(username_or_email: String, password: String) -> Result<bool, ServerFnError> {
|
||||
pub async fn login(credentials: UserCredentials) -> Result<bool, ServerFnError> {
|
||||
use crate::users::validate_user;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
let possible_user = validate_user(username_or_email, password).await
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
let user = validate_user(credentials).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error validating user: {}", e)))?;
|
||||
|
||||
let user = match possible_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(false)
|
||||
};
|
||||
|
||||
/*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))),
|
||||
}*/
|
||||
|
||||
Ok(false)
|
||||
if let Some(user) = user {
|
||||
auth_session.login(&user).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e)))?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user out
|
||||
/// Returns a Result with the error message if the user could not be logged out
|
||||
#[server(endpoint = "logout")]
|
||||
pub async fn logout() -> Result<(), ServerFnError> {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
/*match extract::<Option<Identity>>().await {
|
||||
Ok(Some(user)) => user.logout(),
|
||||
Ok(None) => {},
|
||||
Err(e) => return Err(ServerFnError::<NoCustomError>::ServerError(format!("Error getting user: {}", e))),
|
||||
};*/
|
||||
auth_session.logout().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user