Documentation
¶
Index ¶
- func SessionChecker[T any](next http.Handler, store *SessionStore) http.Handler
- type Config
- func (c Config) WithContextName(val http.SameSite) Config
- func (c Config) WithCookieHttpOnly(val bool) Config
- func (c Config) WithCookieName(name string) Config
- func (c Config) WithCookiePath(path string) Config
- func (c Config) WithCookieSameSite(val http.SameSite) Config
- func (c Config) WithCookieSecure(val bool) Config
- func (c Config) WithDir(path string) Config
- func (c Config) WithExtendSessions(val bool) Config
- func (c Config) WithInMemory(val bool) Config
- func (c Config) WithSessionLength(length time.Duration) Config
- type SessionStore
- func (s *SessionStore) Close() error
- func (s *SessionStore) Delete(sessionId string) error
- func (s *SessionStore) DeleteWithCookie(w http.ResponseWriter, r *http.Request) error
- func (s *SessionStore) Get(sessionId string, v any) error
- func (s *SessionStore) GetWithCookie(w http.ResponseWriter, r *http.Request, v any) error
- func (s *SessionStore) New(data any) (string, error)
- func (s *SessionStore) NewWithCookie(w http.ResponseWriter, data any) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SessionChecker ¶
SessionChecker checks if a session cookie exists, gets the session if it exists and adds it to the request context.
SessionChecker is a generic function that expects a session data type so it can extract the session data properly.
Types ¶
type Config ¶
type Config struct {
// the directory in which the persistent session store is located.
Dir string
// if true, the session store will only be in memory, not persistent.
InMemory bool
// the duration of a single session.
SessionLength time.Duration
// automatically renew sessions on successful validation.
ExtendSessions bool
// the name of the session cookie
CookieName string
// the path of the cookie
CookiePath string
// if true, the cookie is designated http only
CookieHttpOnly bool
// if true, the cookie is designated secure only
CookieSecure bool
// sets the same site protocol for the cookie
CookieSameSite http.SameSite
// sets the name of the session on the context
ContextName string
}
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a session store config with sensible defaults. Modify these to suit using the `WithX` chain methods.
func (Config) WithContextName ¶
WithContextName returns a new Config with ContextName set to the given value.
The default value of ContextName is session.
func (Config) WithCookieHttpOnly ¶
WithCookieHttpOnly returns a new Config with CookieHttpOnly set to the given value.
The default value of CookieHttpOnly is true.
func (Config) WithCookieName ¶
WithCookieName returns a new Config with CookieName set to the given value.
The default value of CookieName is session.
func (Config) WithCookiePath ¶
WithCookiePath returns a new Config with CookiePath set to the given value.
The default value of CookiePath is /.
func (Config) WithCookieSameSite ¶
WithCookieSameSite returns a new Config with CookieSameSite set to the given value.
The default value of CookieSameSite is Strict.
func (Config) WithCookieSecure ¶
WithCookieSecure returns a new Config with CookieSecure set to the given value.
The default value of CookieSecure is true.
func (Config) WithDir ¶
WithDir returns a new Config with Dir set to the given value.
The default value of Dir is set to `/data`.
func (Config) WithExtendSessions ¶
WithExtendSessions returns a new Config with ExtendSessions set to the given value.
The default value of ExtendSessions is true.
func (Config) WithInMemory ¶
WithInMemory returns a new Config with InMemory set to the given value.
The default value of InMemory is set to false.
type SessionStore ¶
type SessionStore struct {
// contains filtered or unexported fields
}
func NewSessionStore ¶
func NewSessionStore(config Config) (*SessionStore, error)
NewSessionStore returns a session store database connection.
func (*SessionStore) Close ¶
func (s *SessionStore) Close() error
closes the connection to the session store database.
func (*SessionStore) Delete ¶
func (s *SessionStore) Delete(sessionId string) error
Delete removes a session from the session store if it exists.
func (*SessionStore) DeleteWithCookie ¶
func (s *SessionStore) DeleteWithCookie(w http.ResponseWriter, r *http.Request) error
Deletes the session from the session store and invalidates the session cookie on the response writer.
func (*SessionStore) Get ¶
func (s *SessionStore) Get(sessionId string, v any) error
Get retrieves the given session ID and stores the session data in the value pointer v.
func (*SessionStore) GetWithCookie ¶
func (s *SessionStore) GetWithCookie(w http.ResponseWriter, r *http.Request, v any) error
func (*SessionStore) New ¶
func (s *SessionStore) New(data any) (string, error)
New adds a new session to the store along with the given data and returns a session ID.
func (*SessionStore) NewWithCookie ¶
func (s *SessionStore) NewWithCookie(w http.ResponseWriter, data any) (string, error)
NewHttp creates a new session, sets a new session cookie on the response writer, and returns the session ID.