Skip to main content

openauth_core/options/
email_password.rs

1use std::fmt;
2use std::sync::Arc;
3
4use http::Request;
5
6use crate::db::User;
7use crate::error::OpenAuthError;
8
9/// Payload passed when an existing user attempts email/password sign-up.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct ExistingUserSignUpPayload {
12    pub user: User,
13}
14
15/// Hook invoked for protected duplicate sign-up attempts.
16pub trait OnExistingUserSignUp: Send + Sync + 'static {
17    fn on_existing_user_sign_up(
18        &self,
19        payload: ExistingUserSignUpPayload,
20        request: Option<&Request<Vec<u8>>>,
21    ) -> Result<(), OpenAuthError>;
22}
23
24impl<F> OnExistingUserSignUp for F
25where
26    F: for<'a> Fn(
27            ExistingUserSignUpPayload,
28            Option<&'a Request<Vec<u8>>>,
29        ) -> Result<(), OpenAuthError>
30        + Send
31        + Sync
32        + 'static,
33{
34    fn on_existing_user_sign_up(
35        &self,
36        payload: ExistingUserSignUpPayload,
37        request: Option<&Request<Vec<u8>>>,
38    ) -> Result<(), OpenAuthError> {
39        self(payload, request)
40    }
41}
42
43/// Email/password authentication configuration.
44#[derive(Clone)]
45pub struct EmailPasswordOptions {
46    pub enabled: bool,
47    pub disable_sign_up: bool,
48    pub auto_sign_in: bool,
49    pub require_email_verification: bool,
50    pub on_existing_user_sign_up: Option<Arc<dyn OnExistingUserSignUp>>,
51}
52
53impl Default for EmailPasswordOptions {
54    fn default() -> Self {
55        Self {
56            enabled: true,
57            disable_sign_up: false,
58            auto_sign_in: true,
59            require_email_verification: false,
60            on_existing_user_sign_up: None,
61        }
62    }
63}
64
65impl EmailPasswordOptions {
66    pub fn new() -> Self {
67        Self::default()
68    }
69
70    pub fn builder() -> Self {
71        Self::new()
72    }
73
74    #[must_use]
75    pub fn enabled(mut self, enabled: bool) -> Self {
76        self.enabled = enabled;
77        self
78    }
79
80    #[must_use]
81    pub fn disable_sign_up(mut self, disabled: bool) -> Self {
82        self.disable_sign_up = disabled;
83        self
84    }
85
86    #[must_use]
87    pub fn auto_sign_in(mut self, enabled: bool) -> Self {
88        self.auto_sign_in = enabled;
89        self
90    }
91
92    #[must_use]
93    pub fn require_email_verification(mut self, required: bool) -> Self {
94        self.require_email_verification = required;
95        self
96    }
97
98    #[must_use]
99    pub fn on_existing_user_sign_up<H>(mut self, handler: H) -> Self
100    where
101        H: OnExistingUserSignUp,
102    {
103        self.on_existing_user_sign_up = Some(Arc::new(handler));
104        self
105    }
106}
107
108impl fmt::Debug for EmailPasswordOptions {
109    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
110        formatter
111            .debug_struct("EmailPasswordOptions")
112            .field("enabled", &self.enabled)
113            .field("disable_sign_up", &self.disable_sign_up)
114            .field("auto_sign_in", &self.auto_sign_in)
115            .field(
116                "require_email_verification",
117                &self.require_email_verification,
118            )
119            .field(
120                "on_existing_user_sign_up",
121                &self
122                    .on_existing_user_sign_up
123                    .as_ref()
124                    .map(|_| "<on-existing-user-sign-up>"),
125            )
126            .finish()
127    }
128}