Skip to main content

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.

note

Echo community contribution

Dependencies

import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
)

Usage

e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))

e.GET("/", func(c echo.Context) error {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
sess.Values["foo"] = "bar"
sess.Save(c.Request(), c.Response())
return c.NoContent(http.StatusOK)
})

Custom Configuration

Usage

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

Configuration

Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper

// Session store.
// Required.
Store sessions.Store
}

Default Configuration

DefaultConfig = Config{
Skipper: DefaultSkipper,
}