@@ -32,6 +32,10 @@ type SessionManager struct {
32
32
// Cookie contains the configuration settings for session cookies.
33
33
Cookie SessionCookie
34
34
35
+ // CookieFunc allows you to set update attributes on the session cookie
36
+ // before it gets written to the http-response.
37
+ CookieFunc func (* http.Request , * http.Cookie )
38
+
35
39
// Codec controls the encoder/decoder used to transform session data to a
36
40
// byte slice for use by the session store. By default session data is
37
41
// encoded/decoded using encoding/gob.
@@ -107,6 +111,7 @@ func New() *SessionManager {
107
111
Codec : GobCodec {},
108
112
ErrorFunc : defaultErrorFunc ,
109
113
contextKey : generateContextKey (),
114
+ CookieFunc : defaultCookieFunc ,
110
115
Cookie : SessionCookie {
111
116
Name : "session" ,
112
117
Domain : "" ,
@@ -172,9 +177,9 @@ func (s *SessionManager) commitAndWriteSessionCookie(w http.ResponseWriter, r *h
172
177
return
173
178
}
174
179
175
- s .WriteSessionCookie (ctx , w , token , expiry )
180
+ s .WriteSessionCookie (ctx , w , r , token , expiry )
176
181
case Destroyed :
177
- s .WriteSessionCookie (ctx , w , "" , time.Time {})
182
+ s .WriteSessionCookie (ctx , w , r , "" , time.Time {})
178
183
}
179
184
}
180
185
@@ -188,7 +193,7 @@ func (s *SessionManager) commitAndWriteSessionCookie(w http.ResponseWriter, r *h
188
193
//
189
194
// Most applications will use the LoadAndSave() middleware and will not need to
190
195
// use this method.
191
- func (s * SessionManager ) WriteSessionCookie (ctx context.Context , w http.ResponseWriter , token string , expiry time.Time ) {
196
+ func (s * SessionManager ) WriteSessionCookie (ctx context.Context , w http.ResponseWriter , r * http. Request , token string , expiry time.Time ) {
192
197
cookie := & http.Cookie {
193
198
Name : s .Cookie .Name ,
194
199
Value : token ,
@@ -198,6 +203,7 @@ func (s *SessionManager) WriteSessionCookie(ctx context.Context, w http.Response
198
203
HttpOnly : s .Cookie .HttpOnly ,
199
204
SameSite : s .Cookie .SameSite ,
200
205
}
206
+ s .CookieFunc (r , cookie )
201
207
202
208
if expiry .IsZero () {
203
209
cookie .Expires = time .Unix (1 , 0 )
@@ -216,6 +222,9 @@ func defaultErrorFunc(w http.ResponseWriter, r *http.Request, err error) {
216
222
http .Error (w , http .StatusText (http .StatusInternalServerError ), http .StatusInternalServerError )
217
223
}
218
224
225
+ func defaultCookieFunc (r * http.Request , c * http.Cookie ) {
226
+ }
227
+
219
228
type sessionResponseWriter struct {
220
229
http.ResponseWriter
221
230
request * http.Request
0 commit comments