use crate::models::PostLoginAction;
use crate::repositories::{CredentialRepository, CredentialType, TotpRepository, UserEntity};
use crate::services::SettingsService;
pub async fn compute_post_login(
user: &UserEntity,
settings: &SettingsService,
totp_repo: &dyn TotpRepository,
credential_repo: &dyn CredentialRepository,
) -> Option<PostLoginAction> {
let mfa_required = settings
.get_bool("security_require_mfa")
.await
.unwrap_or(None);
if mfa_required == Some(true) {
let has_mfa = totp_repo.has_mfa_enabled(user.id).await.unwrap_or(false);
if !has_mfa {
let has_password = credential_repo
.has_credential_type(user.id, CredentialType::Password)
.await
.unwrap_or(false);
if has_password {
return Some(PostLoginAction {
action: "setup_mfa".to_string(),
redirect_url: None,
});
}
}
}
let welcome_enabled = settings
.get_bool("postlogin_welcome_enabled")
.await
.unwrap_or(None);
if welcome_enabled == Some(true) && user.welcome_completed_at.is_none() {
let route = settings
.get("postlogin_welcome_route")
.await
.unwrap_or(None)
.filter(|r| !r.is_empty())
.unwrap_or_else(|| "/welcome".to_string());
return Some(PostLoginAction {
action: "welcome".to_string(),
redirect_url: Some(route),
});
}
let complete_enabled = settings
.get_bool("postlogin_complete_enabled")
.await
.unwrap_or(None);
if complete_enabled == Some(true) && user.name.is_none() {
return Some(PostLoginAction {
action: "complete_profile".to_string(),
redirect_url: None,
});
}
let redirect_url = settings
.get("postlogin_redirect_url")
.await
.unwrap_or(None)
.filter(|url| !url.is_empty());
if let Some(url) = redirect_url {
return Some(PostLoginAction {
action: "redirect".to_string(),
redirect_url: Some(url),
});
}
None
}