Skip to main content

haste_server/auth_n/session/
user.rs

1use haste_fhir_model::r4::generated::terminology::IssueType;
2use haste_fhir_operation_error::OperationOutcomeError;
3use haste_repository::types::user::User;
4use tower_sessions::Session;
5
6static USER_KEY: &str = "auth_user";
7
8pub async fn get_user(session: &Session) -> Result<Option<User>, OperationOutcomeError> {
9    let user = session.get::<User>(USER_KEY).await.map_err(|_e| {
10        OperationOutcomeError::fatal(
11            IssueType::Exception(None),
12            "Session returned an error when retrieving current user.".to_string(),
13        )
14    })?;
15
16    Ok(user)
17}
18
19pub async fn set_user(session: &Session, user: &User) -> Result<(), OperationOutcomeError> {
20    session.insert(USER_KEY, user).await.map_err(|_e| {
21        OperationOutcomeError::fatal(
22            IssueType::Exception(None),
23            "Failed to set user in session.".to_string(),
24        )
25    })
26}
27
28pub async fn clear_user(session: &Session) -> Result<(), OperationOutcomeError> {
29    session.remove::<User>(USER_KEY).await.map_err(|_e| {
30        OperationOutcomeError::fatal(
31            IssueType::Exception(None),
32            "Failed to clear user from session.".to_string(),
33        )
34    })?;
35
36    Ok(())
37}