SaveIfWritten session method
This commit is contained in:
parent
c7122a591b
commit
07313fbda4
@ -57,26 +57,14 @@ func expectAppSession(next handler.Private) handler.PublicFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveWrittenSession(hnd handler.PublicFunc) func(s *session.Session, w http.ResponseWriter, r *http.Request) rsvp.Response {
|
|
||||||
return func(s *session.Session, w http.ResponseWriter, r *http.Request) rsvp.Response {
|
|
||||||
response := hnd(s, w.Header(), r)
|
|
||||||
|
|
||||||
if s.WasWritten() {
|
|
||||||
err := s.Save(r, w)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to save session: %s\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Private) handleWithSession(pattern string, hnd handler.PublicFunc) {
|
func (m *Private) handleWithSession(pattern string, hnd handler.PublicFunc) {
|
||||||
m.inner.Std.HandleFunc(pattern, rsvp.MiddlewareBeforeWriteResponse(m.inner.Config, rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response {
|
getSessionMiddleware := rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response {
|
||||||
s := m.GetSession(r)
|
s := m.GetSession(r)
|
||||||
return saveWrittenSession(hnd)(s, w, r)
|
saveIfWritten := s.SaveIfWrittenMiddleware(hnd)
|
||||||
})))
|
return saveIfWritten(w, r)
|
||||||
|
})
|
||||||
|
beforeWrite := rsvp.MiddlewareBeforeWriteResponse(m.inner.Config, getSessionMiddleware)
|
||||||
|
m.inner.Std.HandleFunc(pattern, beforeWrite)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Private) Handle(pattern string, handler handler.Private) {
|
func (m *Private) Handle(pattern string, handler handler.Private) {
|
||||||
|
|||||||
@ -41,11 +41,11 @@ func (m *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handler := rsvp.MiddlewareBeforeWriteResponse(&m.rsvpCfg, rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response {
|
handler := rsvp.MiddlewareBeforeWriteResponse(&m.rsvpCfg, rsvp.MiddleHandlerFunc(func(w http.ResponseWriter, r *http.Request) rsvp.Response {
|
||||||
sessionWriter := saveWrittenSession(func(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
|
sessionWriter := s.SaveIfWrittenMiddleware(func(s *session.Session, h http.Header, r *http.Request) rsvp.Response {
|
||||||
return routing.LoginWithCreds(username, password, s, h, r)
|
return routing.LoginWithCreds(username, password, s, h, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
return sessionWriter(s, w, r)
|
return sessionWriter(w, r)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
handler(w, r)
|
handler(w, r)
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Teajey/rsvp"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,6 +17,21 @@ func New(inner *sessions.Session) *Session {
|
|||||||
return &Session{inner: inner}
|
return &Session{inner: inner}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) SaveIfWrittenMiddleware(next func(*Session, http.Header, *http.Request) rsvp.Response) rsvp.MiddleHandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) rsvp.Response {
|
||||||
|
response := next(s, w.Header(), r)
|
||||||
|
|
||||||
|
if s.WasWritten() {
|
||||||
|
err := s.Save(r, w)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to save session: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const flashKey = "_flash"
|
const flashKey = "_flash"
|
||||||
|
|
||||||
func (s *Session) FlashGet() any {
|
func (s *Session) FlashGet() any {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user