use std::sync::Arc;
use better_auth_core::{
AuthPlugin, AuthResult, AuthSchema, FieldValue, background::BackgroundFuture,
};
use super::TwoFactorPlugin;
use crate::plugins::endpoint_context::{EndpointContext, WithCallbacks};
type Sender<S> = dyn Fn(&FieldValue, &str, &EndpointContext<'_, S>) -> AuthResult<Option<BackgroundFuture>>
+ Send
+ Sync;
pub struct TwoFactorCallbacks<S: AuthSchema> {
pub(super) sender: Option<Arc<Sender<S>>>,
}
impl<S: AuthSchema> Default for TwoFactorCallbacks<S> {
fn default() -> Self {
Self { sender: None }
}
}
impl<S: AuthSchema> TwoFactorCallbacks<S> {
pub fn send<F>(mut self, callback: F) -> Self
where
F: Fn(&FieldValue, &str, &EndpointContext<'_, S>) -> AuthResult<Option<BackgroundFuture>>
+ Send
+ Sync
+ 'static,
{
self.sender = Some(Arc::new(callback));
self
}
}
impl TwoFactorPlugin {
pub fn callbacks<S: AuthSchema>(self, callbacks: TwoFactorCallbacks<S>) -> impl AuthPlugin<S> {
WithCallbacks {
plugin: self,
callbacks: Arc::new(callbacks),
}
}
}