Add API endpoint to get logged in user

This commit is contained in:
Ethan Girouard 2024-11-03 16:59:23 -05:00
parent 251d567863
commit 43a5b519fd
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146

View File

@ -147,6 +147,19 @@ pub async fn get_user() -> Result<User, ServerFnError> {
auth_session.user.ok_or(ServerFnError::<NoCustomError>::ServerError("User not logged in".to_string())) auth_session.user.ok_or(ServerFnError::<NoCustomError>::ServerError("User not logged in".to_string()))
} }
#[server(endpoint = "get_logged_in_user")]
pub async fn get_logged_in_user() -> Result<Option<User>, ServerFnError> {
let auth_session = extract::<AuthSession<AuthBackend>>().await
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
let user = auth_session.user.map(|mut user| {
user.password = None;
user
});
Ok(user)
}
/// Check if a user is an admin /// Check if a user is an admin
/// Returns a Result with a boolean indicating if the user is logged in and an admin /// Returns a Result with a boolean indicating if the user is logged in and an admin
#[server(endpoint = "check_admin")] #[server(endpoint = "check_admin")]