use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthSession {
Anonymous(AnonymousUser),
Authenticated(AuthenticatedUser),
}
impl Default for AuthSession {
fn default() -> Self {
Self::Anonymous(AnonymousUser::default())
}
}
impl AuthSession {
pub fn is_authenticated(&self) -> bool {
matches!(self, Self::Authenticated(_))
}
pub fn user(&self) -> Option<&AuthenticatedUser> {
match self {
Self::Authenticated(user) => Some(user),
Self::Anonymous(_) => None,
}
}
pub fn display_label(&self) -> String {
match self {
Self::Authenticated(user) => user
.display_name
.as_ref()
.or(user.email.as_ref())
.cloned()
.unwrap_or_else(|| user.user_id.clone()),
Self::Anonymous(_) => "Guest".to_string(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnonymousUser {
pub reason: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthenticatedUser {
pub user_id: String,
pub email: Option<String>,
pub display_name: Option<String>,
pub avatar_url: Option<String>,
pub roles: Vec<String>,
pub email_verified: bool,
}