mirror of
https://github.com/alexedwards/scs.git
synced 2025-07-15 01:04:36 +02:00
Refactor to remove writeSessionCookie() method
This commit is contained in:
52
session.go
52
session.go
@ -147,29 +147,9 @@ func (s *SessionManager) LoadAndSave(next http.Handler) http.Handler {
|
|||||||
sr.MultipartForm.RemoveAll()
|
sr.MultipartForm.RemoveAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch s.Status(ctx) {
|
if s.Status(ctx) != Unmodified {
|
||||||
case Modified:
|
responseCookie := &http.Cookie{
|
||||||
token, expiry, err := s.Commit(ctx)
|
|
||||||
if err != nil {
|
|
||||||
s.ErrorFunc(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.writeSessionCookie(w, token, expiry)
|
|
||||||
case Destroyed:
|
|
||||||
s.writeSessionCookie(w, "", time.Time{})
|
|
||||||
}
|
|
||||||
|
|
||||||
if bw.code != 0 {
|
|
||||||
w.WriteHeader(bw.code)
|
|
||||||
}
|
|
||||||
w.Write(bw.buf.Bytes())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SessionManager) writeSessionCookie(w http.ResponseWriter, token string, expiry time.Time) {
|
|
||||||
cookie := &http.Cookie{
|
|
||||||
Name: s.Cookie.Name,
|
Name: s.Cookie.Name,
|
||||||
Value: token,
|
|
||||||
Path: s.Cookie.Path,
|
Path: s.Cookie.Path,
|
||||||
Domain: s.Cookie.Domain,
|
Domain: s.Cookie.Domain,
|
||||||
Secure: s.Cookie.Secure,
|
Secure: s.Cookie.Secure,
|
||||||
@ -177,18 +157,32 @@ func (s *SessionManager) writeSessionCookie(w http.ResponseWriter, token string,
|
|||||||
SameSite: s.Cookie.SameSite,
|
SameSite: s.Cookie.SameSite,
|
||||||
}
|
}
|
||||||
|
|
||||||
if expiry.IsZero() {
|
switch s.Status(ctx) {
|
||||||
cookie.Expires = time.Unix(1, 0)
|
case Modified:
|
||||||
cookie.MaxAge = -1
|
token, expiry, err := s.Commit(ctx)
|
||||||
} else if s.Cookie.Persist {
|
if err != nil {
|
||||||
cookie.Expires = time.Unix(expiry.Unix()+1, 0) // Round up to the nearest second.
|
s.ErrorFunc(w, r, err)
|
||||||
cookie.MaxAge = int(time.Until(expiry).Seconds() + 1) // Round up to the nearest second.
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Add("Set-Cookie", cookie.String())
|
responseCookie.Value = token
|
||||||
|
responseCookie.Expires = time.Unix(expiry.Unix()+1, 0) // Round up to the nearest second.
|
||||||
|
responseCookie.MaxAge = int(time.Until(expiry).Seconds() + 1) // Round up to the nearest second.
|
||||||
|
case Destroyed:
|
||||||
|
responseCookie.Expires = time.Unix(1, 0)
|
||||||
|
responseCookie.MaxAge = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Add("Set-Cookie", responseCookie.String())
|
||||||
addHeaderIfMissing(w, "Cache-Control", `no-cache="Set-Cookie"`)
|
addHeaderIfMissing(w, "Cache-Control", `no-cache="Set-Cookie"`)
|
||||||
addHeaderIfMissing(w, "Vary", "Cookie")
|
addHeaderIfMissing(w, "Vary", "Cookie")
|
||||||
|
}
|
||||||
|
|
||||||
|
if bw.code != 0 {
|
||||||
|
w.WriteHeader(bw.code)
|
||||||
|
}
|
||||||
|
w.Write(bw.buf.Bytes())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func addHeaderIfMissing(w http.ResponseWriter, key, value string) {
|
func addHeaderIfMissing(w http.ResponseWriter, key, value string) {
|
||||||
|
Reference in New Issue
Block a user