SkillAgentSearch skills...

Cookiesession

Go library for managing sessions in cookies

Install / Use

/learn @lstoll/Cookiesession
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

cookiesession

Go Reference

Status: in development

cookiesession is a Go library for easy, cookie-based sessions. It provides a typed interface for getting and setting the session in HTTP handlers, with minimal fuss. Cookies are encrypted and authenticated by default.

Example


// sessionData is the type for our session. It is JSON serialized, so we tag it
// appropriately.
type sessionData struct {
	Name string `json:"name"`
}

// SessionName assigns this session type a unique name, so it can interact with
// other sessions. Multiple Managers in use is possible.
func (sessionData) SessionName() string {
	return "example"
}

// derive a key from a passphrase
keys, _ := KeysFromPassphrases("my-cookie-encryption-passphrase")

// create the manager for our session type
csmgr, _ := New[sessionData](keys, Options{
    Path:   "/",
    MaxAge: 60 * time.Minute,
})

mux := http.NewServeMux()

mux.HandleFunc("POST /set", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
    sess, _ := csmgr.Get(r.Context())

    sess.Name = r.FormValue("name")

    csmgr.Save(r.Context(), sess)
}))

mux.HandleFunc("GET /read", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    sess, _ := csmgr.Get(r.Context())

    w.Write([]byte(sess.Name))
}))

View on GitHub
GitHub Stars4
CategoryDevelopment
Updated2y ago
Forks0

Languages

Go

Security Score

70/100

Audited on Feb 29, 2024

No findings