pub trait UserDetailsService: Send + Sync {
// Required method
fn load_user_by_username<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<User>, UserDetailsError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
// Provided method
fn user_exists<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool, UserDetailsError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
}Expand description
Async trait for loading user details from any data source.
§Spring Security Equivalent
Similar to UserDetailsService in Spring Security.
§Example
ⓘ
use actix_security_core::http::security::user_details::{UserDetailsService, UserDetailsError};
use async_trait::async_trait;
struct DatabaseUserDetailsService {
pool: sqlx::PgPool,
}
#[async_trait]
impl UserDetailsService for DatabaseUserDetailsService {
async fn load_user_by_username(&self, username: &str) -> Result<Option<User>, UserDetailsError> {
let row = sqlx::query!("SELECT * FROM users WHERE username = $1", username)
.fetch_optional(&self.pool)
.await
.map_err(|e| UserDetailsError::StorageError(e.to_string()))?;
Ok(row.map(|r| User::with_encoded_password(&r.username, r.password)
.roles(&r.roles.split(',').map(String::from).collect::<Vec<_>>())))
}
}Required Methods§
Sourcefn load_user_by_username<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<User>, UserDetailsError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn load_user_by_username<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<User>, UserDetailsError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Load user by username.
Returns Ok(Some(user)) if found, Ok(None) if not found,
or Err(...) if an error occurred.
Provided Methods§
Sourcefn user_exists<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool, UserDetailsError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn user_exists<'life0, 'life1, 'async_trait>(
&'life0 self,
username: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool, UserDetailsError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Check if a user exists.