Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 2.31 KB

README.md

File metadata and controls

75 lines (56 loc) · 2.31 KB

cookie-session

Use cookie as session, base on secure cookie to encrypt cookie, you can also use the another library instead of it.

Build Status Coverage Status License GoDoc

Features

  • 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

Installation

go get github.com/go-http-utils/cookie-session

Examples

go run example/main.go

Usage

// 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()
    }
  })

Other Store Implementations

Other Application Implementations