Skip to main content

rs_auth_core/
hooks.rs

1use async_trait::async_trait;
2use tracing::warn;
3
4use crate::error::AuthError;
5use crate::events::AuthEvent;
6
7#[async_trait]
8pub trait AuthHook: Send + Sync {
9    async fn on_event(&self, event: &AuthEvent) -> Result<(), AuthError> {
10        let _ = event;
11        Ok(())
12    }
13}
14
15pub struct EventEmitter {
16    hooks: Vec<Box<dyn AuthHook>>,
17}
18
19impl EventEmitter {
20    pub fn new() -> Self {
21        Self { hooks: vec![] }
22    }
23
24    pub fn add_hook(&mut self, hook: Box<dyn AuthHook>) {
25        self.hooks.push(hook);
26    }
27
28    pub async fn emit(&self, event: AuthEvent) {
29        for hook in &self.hooks {
30            if let Err(e) = hook.on_event(&event).await {
31                warn!(error = %e, "auth hook error");
32            }
33        }
34    }
35}
36
37impl Default for EventEmitter {
38    fn default() -> Self {
39        Self::new()
40    }
41}