rustauth_core/options/
email_password.rs1use std::fmt;
2use std::sync::Arc;
3
4use http::Request;
5
6use crate::db::User;
7use crate::error::RustAuthError;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct ExistingUserSignUpPayload {
12 pub user: User,
13}
14
15pub 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<(), RustAuthError>;
22}
23
24impl<F> OnExistingUserSignUp for F
25where
26 F: for<'a> Fn(
27 ExistingUserSignUpPayload,
28 Option<&'a Request<Vec<u8>>>,
29 ) -> Result<(), RustAuthError>
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<(), RustAuthError> {
39 self(payload, request)
40 }
41}
42
43#[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 pub another_email_error_on_duplicate: bool,
52}
53
54impl Default for EmailPasswordOptions {
55 fn default() -> Self {
56 Self {
57 enabled: false,
58 disable_sign_up: false,
59 auto_sign_in: true,
60 require_email_verification: false,
61 on_existing_user_sign_up: None,
62 another_email_error_on_duplicate: false,
63 }
64 }
65}
66
67impl EmailPasswordOptions {
68 pub fn new() -> Self {
69 Self::default()
70 }
71
72 #[must_use]
73 pub fn enabled(mut self, enabled: bool) -> Self {
74 self.enabled = enabled;
75 self
76 }
77
78 #[must_use]
79 pub fn disable_sign_up(mut self, disabled: bool) -> Self {
80 self.disable_sign_up = disabled;
81 self
82 }
83
84 #[must_use]
85 pub fn auto_sign_in(mut self, enabled: bool) -> Self {
86 self.auto_sign_in = enabled;
87 self
88 }
89
90 #[must_use]
91 pub fn require_email_verification(mut self, required: bool) -> Self {
92 self.require_email_verification = required;
93 self
94 }
95
96 #[must_use]
97 pub fn on_existing_user_sign_up<H>(mut self, handler: H) -> Self
98 where
99 H: OnExistingUserSignUp,
100 {
101 self.on_existing_user_sign_up = Some(Arc::new(handler));
102 self
103 }
104
105 #[must_use]
106 pub fn another_email_error_on_duplicate(mut self, enabled: bool) -> Self {
107 self.another_email_error_on_duplicate = enabled;
108 self
109 }
110}
111
112impl fmt::Debug for EmailPasswordOptions {
113 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
114 formatter
115 .debug_struct("EmailPasswordOptions")
116 .field("enabled", &self.enabled)
117 .field("disable_sign_up", &self.disable_sign_up)
118 .field("auto_sign_in", &self.auto_sign_in)
119 .field(
120 "require_email_verification",
121 &self.require_email_verification,
122 )
123 .field(
124 "on_existing_user_sign_up",
125 &self
126 .on_existing_user_sign_up
127 .as_ref()
128 .map(|_| "<on-existing-user-sign-up>"),
129 )
130 .finish()
131 }
132}