@@ -51,6 +51,7 @@ pub struct SessionMiddleware<Store> {
51
51
store : Store ,
52
52
cookie_path : String ,
53
53
cookie_name : String ,
54
+ cookie_domain : Option < String > ,
54
55
session_ttl : Option < Duration > ,
55
56
save_unchanged : bool ,
56
57
same_site_policy : SameSite ,
@@ -63,6 +64,7 @@ impl<Store: SessionStore> std::fmt::Debug for SessionMiddleware<Store> {
63
64
. field ( "store" , & self . store )
64
65
. field ( "cookie_path" , & self . cookie_path )
65
66
. field ( "cookie_name" , & self . cookie_name )
67
+ . field ( "cookie_domain" , & self . cookie_domain )
66
68
. field ( "session_ttl" , & self . session_ttl )
67
69
. field ( "same_site_policy" , & self . same_site_policy )
68
70
. field ( "key" , & ".." )
@@ -157,6 +159,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
157
159
/// SessionMiddleware::new(MemoryStore::new(), b"please do not hardcode your secret")
158
160
/// .with_cookie_name("custom.cookie.name")
159
161
/// .with_cookie_path("/some/path")
162
+ /// .with_cookie_domain("www.rust-lang.org")
160
163
/// .with_same_site_policy(SameSite::Lax)
161
164
/// .with_session_ttl(Some(Duration::from_secs(1)))
162
165
/// .without_save_unchanged(),
@@ -168,6 +171,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
168
171
save_unchanged : true ,
169
172
cookie_path : "/" . into ( ) ,
170
173
cookie_name : "tide.sid" . into ( ) ,
174
+ cookie_domain : None ,
171
175
same_site_policy : SameSite :: Strict ,
172
176
session_ttl : Some ( Duration :: from_secs ( 24 * 60 * 60 ) ) ,
173
177
key : Key :: derive_from ( secret) ,
@@ -222,6 +226,12 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
222
226
self
223
227
}
224
228
229
+ /// Sets the domain of the cookie.
230
+ pub fn with_cookie_domain ( mut self , cookie_domain : impl AsRef < str > ) -> Self {
231
+ self . cookie_domain = Some ( cookie_domain. as_ref ( ) . to_owned ( ) ) ;
232
+ self
233
+ }
234
+
225
235
//--- methods below here are private ---
226
236
227
237
async fn load_or_create ( & self , cookie_value : Option < String > ) -> Session {
@@ -247,6 +257,10 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
247
257
cookie. set_expires ( Some ( ( std:: time:: SystemTime :: now ( ) + ttl) . into ( ) ) ) ;
248
258
}
249
259
260
+ if let Some ( cookie_domain) = self . cookie_domain . clone ( ) {
261
+ cookie. set_domain ( cookie_domain)
262
+ }
263
+
250
264
self . sign_cookie ( & mut cookie) ;
251
265
252
266
cookie
0 commit comments