Session

Session middleware facilitates HTTP session management backed by gorilla sessions. The default implementation provides cookie and filesystem based session store; however, you can take advantage of community maintained implementation for various backends.

Session - 图1note

Echo community contribution

Dependencies

  1. import (
  2. "github.com/gorilla/sessions"
  3. "github.com/labstack/echo-contrib/session"
  4. )

Usage

  1. e := echo.New()
  2. e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
  3. e.GET("/", func(c echo.Context) error {
  4. sess, _ := session.Get("session", c)
  5. sess.Options = &sessions.Options{
  6. Path: "/",
  7. MaxAge: 86400 * 7,
  8. HttpOnly: true,
  9. }
  10. sess.Values["foo"] = "bar"
  11. sess.Save(c.Request(), c.Response())
  12. return c.NoContent(http.StatusOK)
  13. })

Custom Configuration

Usage

  1. e := echo.New()
  2. e.Use(session.MiddlewareWithConfig(session.Config{}))

Configuration

  1. Config struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper middleware.Skipper
  4. // Session store.
  5. // Required.
  6. Store sessions.Store
  7. }

Default Configuration

  1. DefaultConfig = Config{
  2. Skipper: DefaultSkipper,
  3. }