use super::EmailVerificationPlugin;
use crate::plugins::endpoint_context::{EndpointContext, WithCallbacks};
use better_auth_core::{
AuthPlugin, AuthResult, AuthSchema, background::BackgroundFuture, wire::UserView,
};
use std::sync::Arc;
#[derive(Clone)]
pub struct VerificationEmail {
pub user: UserView,
pub url: String,
pub token: String,
}
type Sender<S> = dyn Fn(&VerificationEmail, &EndpointContext<'_, S>) -> AuthResult<Option<BackgroundFuture>>
+ Send
+ Sync;
pub struct EmailVerificationCallbacks<S: AuthSchema> {
pub(super) sender: Arc<Sender<S>>,
}
impl<S: AuthSchema> EmailVerificationCallbacks<S> {
pub fn send(
callback: impl Fn(
&VerificationEmail,
&EndpointContext<'_, S>,
) -> AuthResult<Option<BackgroundFuture>>
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
sender: Arc::new(callback),
}
}
}
impl EmailVerificationPlugin {
pub fn callbacks<S: AuthSchema>(
self,
callbacks: EmailVerificationCallbacks<S>,
) -> impl AuthPlugin<S> {
WithCallbacks {
plugin: self,
callbacks: Arc::new(callbacks),
}
}
}