You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Corey Daley 4f4a90cc43
Update LICENSE
1 year ago
.github/workflows update GitHub workflows (#268) 1 year ago
vendor bump deps and add vendor dir (#269) 1 year ago
.editorconfig Update go version, add tools for verification and testing (#263) 1 year ago
.gitignore Update go version, add tools for verification and testing (#263) 1 year ago
LICENSE Update LICENSE 1 year ago
Makefile Update go version, add tools for verification and testing (#263) 1 year ago
README.md Add gorilla logo to Readme (#265) 1 year ago
cookie.go Fix linting errors for go1.17 (#253) 3 years ago
cookie_go111.go Fix linting errors for go1.17 (#253) 3 years ago
cookie_go111_test.go Fix linting errors for go1.17 (#253) 3 years ago
cookie_test.go Don't use t.Run in tests, not supported in earlier Go versions 6 years ago
doc.go docs: fix CookieStore creation in doc.go (#206) 5 years ago
go.mod bump deps and add vendor dir (#269) 1 year ago
go.sum bump deps and add vendor dir (#269) 1 year ago
lex.go [refactor] gofmt on lex.go to fix import block. 9 years ago
options.go Fix linting errors for go1.17 (#253) 3 years ago
options_go111.go Fix linting errors for go1.17 (#253) 3 years ago
sessions.go Use golang context pkg instead of gorilla/context to fix memory leaks (#175) 6 years ago
sessions_test.go Update go version, add tools for verification and testing (#263) 1 year ago
store.go Don't propagate "not exist" error if trying to erase a session matchi… (#252) 1 year ago
store_test.go Add missing error check (#123) 7 years ago

README.md

sessions

testing codecov godoc sourcegraph

Gorilla Logo

gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.

The key features are:

  • Simple API: use it as an easy way to set signed (and optionally encrypted) cookies.
  • Built-in backends to store sessions in cookies or the filesystem.
  • Flash messages: session values that last until read.
  • Convenient way to switch session persistency (aka "remember me") and set other attributes.
  • Mechanism to rotate authentication and encryption 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.

Let's start with an example that shows the sessions API in a nutshell:

	import (
		"net/http"
		"github.com/gorilla/sessions"
	)

	// Note: Don't store your key in your source code. Pass it via an
	// environmental variable, or flag (or both), and don't accidentally commit it
	// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
	var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))

	func MyHandler(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		err := session.Save(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

First we initialize a session store calling NewCookieStore() and passing a secret key used to authenticate the session. Inside the handler, we call store.Get() to retrieve an existing session or create a new one. Then we set some session values in session.Values, which is a map[interface{}]interface{}. And finally we call session.Save() to save the session in the response.

More examples are available on the Gorilla website.

Store Implementations

Other implementations of the sessions.Store interface:

License

BSD licensed. See the LICENSE file for details.