Documentation ¶
Overview ¶
Package session provides simple server side session handling.
Index ¶
- func AccessMiddleware(next http.Handler, redirect RedirectFunc) http.Handler
- func Middleware(next HandlerFunc) http.Handler
- func New(cookieName string, maxLifetime int, provider Provider) error
- type HandlerFunc
- type MemProvider
- type Provider
- type RedirectFunc
- type Session
- func (s *Session) Active() bool
- func (s *Session) Destroy(w http.ResponseWriter, r *http.Request) error
- func (s *Session) Get(key string, value interface{}) error
- func (s *Session) Remove(key string)
- func (s *Session) Renew() error
- func (s *Session) Save() error
- func (s *Session) Set(key string, value interface{}) error
- func (s *Session) Token() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessMiddleware ¶
func AccessMiddleware(next http.Handler, redirect RedirectFunc) http.Handler
Middleware to check if session is set. Pass the next http.Handler to be called and a redirect function, which is called when session is not set. If you pass nil for the redirect function, it will return nothing to the client.
func Middleware ¶
func Middleware(next HandlerFunc) http.Handler
Middleware to inject current session.
func New ¶
Creates a new session manager with given cookie name and max lifetime in seconds. The cookie name must not be an emtpy string, the lifetime must be greater than 0 and a provider must be passed. If not, an error will be returned.
Types ¶
type HandlerFunc ¶
type HandlerFunc func(Session, http.ResponseWriter, *http.Request)
Handler function with session injected.
type MemProvider ¶
type MemProvider struct { Provider // contains filtered or unexported fields }
MemProvider provides a in memory solution to store sessions. Use the NewMemProvider() function to create a new instance and pass it to Manager. The methods should not be called manually!
func NewMemProvider ¶
func NewMemProvider() *MemProvider
Creates a new in memory provider for the session manager.
func (*MemProvider) Destroy ¶
func (p *MemProvider) Destroy(session *Session) error
Removes a session from memory.
func (*MemProvider) GC ¶
func (p *MemProvider) GC()
Iterates over all existing sessions in memory and removes sessions which exceeded their lifetime.
func (*MemProvider) Init ¶
Stores a new session in memory with given token and lifetime.
func (*MemProvider) Read ¶
func (p *MemProvider) Read(token string) (Session, error)
Reads a session from memory by given token.
func (*MemProvider) Write ¶
func (p *MemProvider) Write(session *Session) error
Updates a session in memory.
type Provider ¶
type Provider interface { Init(string, time.Time) (Session, error) Read(string) (Session, error) Write(*Session) error Destroy(*Session) error GC() }
A provider abstracts the session backend used by the manager, so that different kinds of storages can be used. For an example see MemProvider.
type RedirectFunc ¶
type RedirectFunc func(http.ResponseWriter, *http.Request) bool
Redirect function in case the session is not set. If true is returned, the middleware will continue calling the next http.Handler.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session provides access to user context related data. Generic data can be set and obtained and will be available on page switch. Call Save() to make sure the data is stored. A session must be initialized by the session manager, to make sure it can be used later.
func GetCurrentSession ¶
Returns the session for the http.Request. If not found, an empty session and an error will be returned.
func GetSession ¶
Returns a session by session token if found. If not, an empty session and an error will be returned.
func NewSession ¶
Creates a new session and returns it. If the http.ResponseWriter or http.Request is not set, the session token won't be stored in cookie. So to create a new session without using http, call it with both nil as parameters.
func (*Session) Destroy ¶
Destroys and invalidates the session. http.ResponseWriter and http.Request are optional, if both are set, the session cookie will be destroyed (which is recommended).
func (*Session) Get ¶
Stores session variable for given key within given variable. When the variable cannot be found, is not assignable (not a pointer) or is nil, an error will be returned. If the type of of the variable found does not match the type of the variable it will be stored in, this method will panic.
func (*Session) Remove ¶
Removes session variable for given key. Use the Save() method to remove it persistently.
func (*Session) Save ¶
Saves the session. Call when data was changed using the Set() method to make it available on next call.
func (*Session) Set ¶
Sets session variable for given key. If a variable is stored for that key already, it will be replaced. Use the Save() method to store it for later usage and obtain it with Get().