-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_utils.go
More file actions
30 lines (28 loc) · 936 Bytes
/
session_utils.go
File metadata and controls
30 lines (28 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package gobookmarks
import (
"errors"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"log"
"net/http"
)
// sanitizeSession returns a new session when err indicates the cookie was
// invalid. The original session is cleared so the client replaces it.
func sanitizeSession(w http.ResponseWriter, r *http.Request, session *sessions.Session, err error) (*sessions.Session, error) {
if err == nil {
return session, nil
}
scErr := new(securecookie.MultiError)
if (errors.As(err, scErr) && scErr.IsDecode() && !scErr.IsInternal() && !scErr.IsUsage()) || errors.Is(err, securecookie.ErrMacInvalid) {
log.Printf("session error: %v", err)
if session != nil {
session.Options.MaxAge = -1
if saveErr := session.Save(r, w); saveErr != nil {
log.Printf("session clear error: %v", saveErr)
}
}
session, _ = SessionStore.New(r, Config.GetSessionName())
return session, nil
}
return session, err
}