sessionstore

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2022 License: MIT Imports: 10 Imported by: 0

README

sessionstore

Go Reference

This is a pure Go session store implementation. A session manager is use to create and remove sessions, add, edit and remove values bound to a session. This can be used to contextualize sessions, e.g. for logically separating user login and admin login or shoping carts or ....

Prerequisites

  • You should have a basic knowledge of how sessions and cookies work.

Usage

SessionManager

As initialization, create a new SessionManager with a supplied session cookie name:

var sessMgr *sessionstore.SessionManager = sessionstore.NewManager("MY_SESSION_NAME")
Session

Then, you can create a Session with a supplied validity, e.g. 30 days:

sess, err := sessMgr.CreateSession(time.Now().Add(30 * 24 * time.Hour))

Retrieve a Session by ID (typically obtained from a session cookie):

sess, err := sessMgr.GetSession("123abc")

Remove a Session by ID (when a user logs out):

err := sessMgr.RemoveSession("123abc")
Session Variables/Values

Add or set a value to a session (not SessionManager!) and get it:

sess.SetVar("key", "value")

val, exists := sess.GetVar("key")
Cookies

coming soon


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	MessageType MessageType
	Content     string
}

type MessageType added in v2.2.0

type MessageType string
const (
	MessageSuccess MessageType = "success"
	MessageError   MessageType = "error"
	MessageWarning MessageType = "warning"
	MessageInfo    MessageType = "info"
)

type Session

type Session struct {
	Id       string
	Lifetime time.Time
	Vars     map[string]string
	Message  Message
	// contains filtered or unexported fields
}

func (*Session) GetMessage

func (s *Session) GetMessage() Message

GetMessage returns a previously set message

func (*Session) GetVar

func (s *Session) GetVar(key string) (string, bool)

GetVar returns whether the variable with the given name and the actual value, if it exists

func (*Session) SetMessage

func (s *Session) SetMessage(t MessageType, content string)

SetMessage sets a flash message to the *Session

Deprecated

func (*Session) SetVar

func (s *Session) SetVar(key string, value string)

SetVar sets a attaches a variable with the given name and value

type SessionManager

type SessionManager struct {
	SessionName string
	Sessions    []*Session
	// contains filtered or unexported fields
}

func NewManager

func NewManager(sn string) *SessionManager

NewManager creates and returns a new *SessionManager

func NewManagerFromFile

func NewManagerFromFile(file string) (*SessionManager, error)

func (*SessionManager) AddMessage added in v2.1.0

func (m *SessionManager) AddMessage(w http.ResponseWriter, t MessageType, msg string)

func (*SessionManager) CreateSession

func (m *SessionManager) CreateSession(lt time.Time) (*Session, error)

CreateSession creates a new Session under the *SessionManager

func (*SessionManager) GetCookieValue

func (m *SessionManager) GetCookieValue(r *http.Request) (string, error)

GetCookieValue fetches the session ID from the session cookie of a given request

func (*SessionManager) GetMessage added in v2.1.0

func (*SessionManager) GetSession

func (m *SessionManager) GetSession(id string) (*Session, error)

GetSession retrieves the Session with the supplied session ID

func (*SessionManager) GetSessionFromCookie

func (m *SessionManager) GetSessionFromCookie(r *http.Request) (*Session, error)

GetSessionFromCookie is a convenience method to find an existing session by taking the session ID from the cookie with the name initially set when creating the *SessionManager.

func (*SessionManager) GetSessionFromUrl

func (m *SessionManager) GetSessionFromUrl(u *url.URL) (*Session, error)

GetSessionFromUrl is a convenience method to find an existing session by taking the session ID from a *url.URL query parameter.

func (*SessionManager) RemoveAllSessions

func (m *SessionManager) RemoveAllSessions()

RemoveAllSessions removes all Sessions from a *SessionManager

func (*SessionManager) RemoveCookie

func (m *SessionManager) RemoveCookie(w http.ResponseWriter, name string)

RemoveCookie is a convenience method to remove the session cookie (s

func (*SessionManager) RemoveSession

func (m *SessionManager) RemoveSession(id string) error

RemoveSession removes the Session with the supplied session ID

func (*SessionManager) SetCookie

func (m *SessionManager) SetCookie(w http.ResponseWriter, value string, expires time.Time)

SetCookie is a convenience method to set a session cookie with the initially chosen name.

func (*SessionManager) ToFile

func (m *SessionManager) ToFile(file string) error

Jump to

Keyboard shortcuts

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