session

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 5 Imported by: 0

README

Echo Session

test MIT License Go Reference

This is session middleware for Echo, provided as an alternative implementation inspired by labstack/echo-contrib/session.

Usage

package main

import (
	"fmt"
	"net/http"

	session "github.com/kohkimakimoto/echo-session"
	"github.com/labstack/echo/v4"
)

func main() {
	e := echo.New()

	e.Use(session.Middleware(session.NewCookieStore([]byte("12345678901234567890123456789012"))))

	e.GET("/", func(c echo.Context) error {
		s := session.MustGet(c)
		counter := 0
		if val := s.Get("counter"); val != nil {
			if count, ok := val.(int); ok {
				counter = count
			}
		}
		counter++

		s.Set("counter", counter)
		if err := s.Save(); err != nil {
			return err
		}
		return c.HTML(http.StatusOK, fmt.Sprintf("Counter: %d", counter))
	})

	e.GET("/refresh", func(c echo.Context) error {
		s := session.MustGet(c)
		s.Clear()
		if err := s.Save(); err != nil {
			return err
		}
		return c.Redirect(http.StatusFound, "/")
	})

	e.Logger.Fatal(e.Start(":8080"))
}

Author

Kohki Makimoto kohki.makimoto@gmail.com

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSession = fmt.Errorf("no session in context")

Functions

func DefaultInvalidSessionErrorHandler

func DefaultInvalidSessionErrorHandler(err error, store gorillasessions.Store, name string, c echo.Context) error

func Middleware

func Middleware(store gorillasessions.Store) echo.MiddlewareFunc

func MiddlewareWithConfig

func MiddlewareWithConfig(config MiddlewareConfig) echo.MiddlewareFunc

func NewCookieStore

func NewCookieStore(secret []byte) *gorillasessions.CookieStore

NewCookieStore creates a new CookieStore using the given secret key. It is a thin wrapper around gorilla/sessions.NewCookieStore with recommended options.

Types

type InvalidSessionErrorHandlerFunc

type InvalidSessionErrorHandlerFunc func(err error, store gorillasessions.Store, name string, c echo.Context) error

type MiddlewareConfig

type MiddlewareConfig struct {
	Skipper middleware.Skipper
	// Store is the session store used to get and save sessions.
	Store gorillasessions.Store
	// Name is the name of the session cookie. The default is "session".
	Name string
	// InvalidSessionErrorHandler is called when an invalid session is detected.
	InvalidSessionErrorHandler InvalidSessionErrorHandlerFunc
}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func Get

func Get(c echo.Context) (*Session, error)

Get retrieves the session from the echo.Context.

func MustGet

func MustGet(c echo.Context) *Session

MustGet retrieves the session from the echo.Context and panics if it fails. If your handlers run after the session middleware, the session should always be available. This function is provided as a convenience to avoid error handling in such cases.

func (*Session) AddFlash

func (s *Session) AddFlash(value any, vars ...string)

func (*Session) Clear

func (s *Session) Clear()

func (*Session) Flashes

func (s *Session) Flashes(vars ...string) []any

func (*Session) Get

func (s *Session) Get(key string) any

func (*Session) GetBool added in v0.2.0

func (s *Session) GetBool(key string) bool

func (*Session) GetInt added in v0.2.0

func (s *Session) GetInt(key string) int

func (*Session) GetInt64 added in v0.2.0

func (s *Session) GetInt64(key string) int64

func (*Session) GetString added in v0.2.0

func (s *Session) GetString(key string) string

func (*Session) Internal

func (s *Session) Internal() *gorillasessions.Session

func (*Session) IsNew

func (s *Session) IsNew() bool

func (*Session) Options

func (s *Session) Options() *gorillasessions.Options

func (*Session) Save

func (s *Session) Save() error

func (*Session) Set

func (s *Session) Set(key string, value any)

func (*Session) Values

func (s *Session) Values() map[any]any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL