torii_axum/
types.rs

1use serde::{Deserialize, Serialize};
2use torii::{Session, User};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct RegisterRequest {
6    pub email: String,
7    pub password: String,
8}
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct LoginRequest {
12    pub email: String,
13    pub password: String,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ChangePasswordRequest {
18    pub old_password: String,
19    pub new_password: String,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct MagicLinkRequest {
24    pub email: String,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct VerifyMagicTokenRequest {
29    pub token: String,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct PasswordResetRequest {
34    pub email: String,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct VerifyResetTokenRequest {
39    pub token: String,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ResetPasswordRequest {
44    pub token: String,
45    pub new_password: String,
46}
47
48#[derive(Debug, Clone, Serialize)]
49pub struct AuthResponse {
50    pub user: User,
51    pub session: Session,
52}
53
54#[derive(Debug, Clone, Serialize)]
55pub struct UserResponse {
56    pub user: User,
57}
58
59#[derive(Debug, Clone, Serialize)]
60pub struct SessionResponse {
61    pub session: Session,
62}
63
64#[derive(Debug, Clone, Serialize)]
65pub struct MessageResponse {
66    pub message: String,
67}
68
69#[derive(Debug, Clone, Serialize)]
70pub struct MagicLinkResponse {
71    pub message: String,
72}
73
74#[derive(Debug, Clone, Serialize)]
75pub struct PasswordResetResponse {
76    pub message: String,
77}
78
79#[derive(Debug, Clone, Serialize)]
80pub struct VerifyResetTokenResponse {
81    pub valid: bool,
82}
83
84#[derive(Debug, Clone, Serialize)]
85pub struct HealthResponse {
86    pub status: String,
87    pub version: String,
88}
89
90#[derive(Debug, Clone)]
91pub struct ConnectionInfo {
92    pub ip: Option<String>,
93    pub user_agent: Option<String>,
94}
95
96#[derive(Debug, Clone)]
97pub struct CookieConfig {
98    pub name: String,
99    pub http_only: bool,
100    pub secure: bool,
101    pub same_site: CookieSameSite,
102    pub path: String,
103}
104
105impl Default for CookieConfig {
106    fn default() -> Self {
107        Self {
108            name: "session_id".to_string(),
109            http_only: true,
110            secure: true,
111            same_site: CookieSameSite::Lax,
112            path: "/".to_string(),
113        }
114    }
115}
116
117#[derive(Debug, Clone)]
118pub enum CookieSameSite {
119    Strict,
120    Lax,
121    None,
122}
123
124/// Configuration for verification links sent via email.
125///
126/// This configuration is used to build URLs for magic link authentication
127/// and password reset flows. The URLs are constructed by combining the
128/// hostname with the path prefix and the specific route.
129///
130/// # Example
131///
132/// ```rust
133/// use torii_axum::LinkConfig;
134///
135/// let config = LinkConfig::new("https://example.com");
136/// // Uses default path_prefix "/auth"
137/// // Magic link URL: https://example.com/auth/magic-link/verify?token=...
138/// // Password reset URL: https://example.com/auth/password/reset?token=...
139///
140/// let config = LinkConfig::new("https://example.com")
141///     .with_path_prefix("/api/v1/auth");
142/// // Magic link URL: https://example.com/api/v1/auth/magic-link/verify?token=...
143/// ```
144#[derive(Debug, Clone)]
145pub struct LinkConfig {
146    /// The base hostname/URL for the application (e.g., "https://example.com")
147    pub hostname: String,
148    /// Path prefix where auth routes are mounted (defaults to "/auth")
149    pub path_prefix: String,
150}
151
152impl LinkConfig {
153    /// Create a new LinkConfig with the given hostname.
154    ///
155    /// The path prefix defaults to "/auth".
156    pub fn new(hostname: impl Into<String>) -> Self {
157        Self {
158            hostname: hostname.into(),
159            path_prefix: "/auth".to_string(),
160        }
161    }
162
163    /// Set a custom path prefix for the auth routes.
164    ///
165    /// Use this if you mount the auth routes at a different path than "/auth".
166    pub fn with_path_prefix(mut self, prefix: impl Into<String>) -> Self {
167        self.path_prefix = prefix.into();
168        self
169    }
170
171    /// Build the magic link verification URL with the given token.
172    ///
173    /// Returns a URL in the format: `{hostname}{path_prefix}/magic-link/verify?token={token}`
174    pub fn magic_link_url(&self, token: &str) -> String {
175        format!(
176            "{}{}/magic-link/verify?token={}",
177            self.hostname.trim_end_matches('/'),
178            self.path_prefix,
179            token
180        )
181    }
182
183    /// Build the password reset URL with the given token.
184    ///
185    /// Returns a URL in the format: `{hostname}{path_prefix}/password/reset?token={token}`
186    pub fn password_reset_url(&self, token: &str) -> String {
187        format!(
188            "{}{}/password/reset?token={}",
189            self.hostname.trim_end_matches('/'),
190            self.path_prefix,
191            token
192        )
193    }
194}
195
196impl Default for CookieSameSite {
197    fn default() -> Self {
198        Self::Lax
199    }
200}
201
202impl CookieConfig {
203    pub fn new(name: impl Into<String>) -> Self {
204        Self {
205            name: name.into(),
206            http_only: true,
207            secure: true,
208            same_site: CookieSameSite::Lax,
209            path: "/".to_string(),
210        }
211    }
212
213    pub fn development() -> Self {
214        Self {
215            name: "session_id".to_string(),
216            http_only: true,
217            secure: false,
218            same_site: CookieSameSite::Lax,
219            path: "/".to_string(),
220        }
221    }
222}