Skip to main content

better_auth_api/plugins/email_verification/
callbacks.rs

1use super::EmailVerificationPlugin;
2use crate::plugins::endpoint_context::{EndpointContext, WithCallbacks};
3use better_auth_core::{
4    AuthPlugin, AuthResult, AuthSchema, background::BackgroundFuture, wire::UserView,
5};
6use std::sync::Arc;
7
8/// Email verification delivery data.
9#[derive(Clone)]
10pub struct VerificationEmail {
11    /// User snapshot supplied by the verification lifecycle.
12    pub user: UserView,
13    /// Complete verification link, including the token.
14    pub url: String,
15    /// Verification token for application-specific delivery.
16    pub token: String,
17}
18type Sender<S> = dyn Fn(&VerificationEmail, &EndpointContext<'_, S>) -> AuthResult<Option<BackgroundFuture>>
19    + Send
20    + Sync;
21
22/// Verification delivery with the complete endpoint and active transaction.
23pub struct EmailVerificationCallbacks<S: AuthSchema> {
24    pub(super) sender: Arc<Sender<S>>,
25}
26impl<S: AuthSchema> EmailVerificationCallbacks<S> {
27    /// Invoke delivery before background scheduling. Return `None` for synchronous completion.
28    /// Use `EndpointContext::to_owned` when the returned future needs its transaction.
29    pub fn send(
30        callback: impl Fn(
31            &VerificationEmail,
32            &EndpointContext<'_, S>,
33        ) -> AuthResult<Option<BackgroundFuture>>
34        + Send
35        + Sync
36        + 'static,
37    ) -> Self {
38        Self {
39            sender: Arc::new(callback),
40        }
41    }
42}
43impl EmailVerificationPlugin {
44    /// Attach typed delivery callbacks after configuring the verification options.
45    pub fn callbacks<S: AuthSchema>(
46        self,
47        callbacks: EmailVerificationCallbacks<S>,
48    ) -> impl AuthPlugin<S> {
49        WithCallbacks {
50            plugin: self,
51            callbacks: Arc::new(callbacks),
52        }
53    }
54}