@@ -51,6 +51,7 @@ pub struct SessionMiddleware<Store> {
5151 store : Store ,
5252 cookie_path : String ,
5353 cookie_name : String ,
54+ cookie_domain : Option < String > ,
5455 session_ttl : Option < Duration > ,
5556 save_unchanged : bool ,
5657 same_site_policy : SameSite ,
@@ -63,6 +64,7 @@ impl<Store: SessionStore> std::fmt::Debug for SessionMiddleware<Store> {
6364 . field ( "store" , & self . store )
6465 . field ( "cookie_path" , & self . cookie_path )
6566 . field ( "cookie_name" , & self . cookie_name )
67+ . field ( "cookie_domain" , & self . cookie_domain )
6668 . field ( "session_ttl" , & self . session_ttl )
6769 . field ( "same_site_policy" , & self . same_site_policy )
6870 . field ( "key" , & ".." )
@@ -157,6 +159,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
157159 /// SessionMiddleware::new(MemoryStore::new(), b"please do not hardcode your secret")
158160 /// .with_cookie_name("custom.cookie.name")
159161 /// .with_cookie_path("/some/path")
162+ /// .with_cookie_domain("www.rust-lang.org")
160163 /// .with_same_site_policy(SameSite::Lax)
161164 /// .with_session_ttl(Some(Duration::from_secs(1)))
162165 /// .without_save_unchanged(),
@@ -168,6 +171,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
168171 save_unchanged : true ,
169172 cookie_path : "/" . into ( ) ,
170173 cookie_name : "tide.sid" . into ( ) ,
174+ cookie_domain : None ,
171175 same_site_policy : SameSite :: Strict ,
172176 session_ttl : Some ( Duration :: from_secs ( 24 * 60 * 60 ) ) ,
173177 key : Key :: derive_from ( secret) ,
@@ -222,6 +226,12 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
222226 self
223227 }
224228
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+
225235 //--- methods below here are private ---
226236
227237 async fn load_or_create ( & self , cookie_value : Option < String > ) -> Session {
@@ -247,6 +257,10 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
247257 cookie. set_expires ( Some ( ( std:: time:: SystemTime :: now ( ) + ttl) . into ( ) ) ) ;
248258 }
249259
260+ if let Some ( cookie_domain) = self . cookie_domain . clone ( ) {
261+ cookie. set_domain ( cookie_domain)
262+ }
263+
250264 self . sign_cookie ( & mut cookie) ;
251265
252266 cookie
0 commit comments