Use cookie as session, base on secure cookie to encrypt cookie, you can also use the another library instead of it.
- Simple API: use it as an easy way to set signed cookies.
- Built-in backends to store sessions in cookies.
- Mechanism to rotate authentication by some custom keys.
- Multiple sessions per request, even using different backends.
- Interfaces and infrastructure for custom session backends: sessions from different stores can be retrieved and batch-saved using a common API.
- User can customize own session with different field that don't require type assertion and cast
go get github.com/go-http-utils/cookie-session
go run example/main.go
// Session is custom by user's business
type Session struct {
*sessions.Meta `json:"-"`
UserID string `json:"userId"`
Name string `json:"name"`
Authed int64 `json:"authed"`
}
func (s *Session) Save() error {
return s.GetStore().Save(s)
}
func (s *Session) Destroy() error {
return s.GetStore().Destroy(s)
}
func main() {
SessionName := "Sess"
SessionKeys := []string{"keyxxx"}
store := sessions.New()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
if session.UserID == "" {
session.UserID = "x"
session.Name = "y"
session.Authed = 1
session.Save()
}
})