Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                

sqlxstore

package
v0.0.0-...-2d41136 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAddingSessionTable            = errors.New("there was an error while trying to add the session table to the database")
	ErrDbConnectionIsNil             = errors.New("the connection struct is nil")
	ErrDbOpenConnection              = errors.New("there was an error while opening the database connection")
	ErrDbPing                        = errors.New("there was an error while pinging the database")
	ErrFailedToDecodeCookie          = errors.New("failed to decode the cookie")
	ErrFailedToDecodeSessionData     = errors.New("failed to decode session data from the database")
	ErrFailedToDeleteExpiredSessions = errors.New("there was an error while trying to delete expired users sessions")
	ErrFailedToDeleteSession         = errors.New("failed to delete a session")
	ErrFailedToEncodeCookie          = errors.New("failed to encode the cookie")
	ErrFailedToEncodeSessionData     = errors.New("failed to encode session data")
	ErrFailedToInsertSession         = errors.New("failed to insert a session in the database")
	ErrFailedToLoadSession           = errors.New("failed to load session")
	ErrFailedToPrepareStmt           = errors.New("there was an error while trying to parse the sql prepared statement")
	ErrFailedToUpdateSession         = errors.New("failed to update a sessionData in the database")
	ErrLoadingSessionDataFromDb      = errors.New("failed to load session data from the database")
	ErrNoParseTimeParameter          = errors.New("you need the parseTime=true parameter in the DSN string")
	ErrNoSessionSaved                = errors.New("there isn't a sessions for that Id in the database")
	ErrSessionExpired                = errors.New("the session is expired")
)

Error definitions.

This is a list of errors that can be returned by the store.

Functions

This section is empty.

Types

type KeyPair

type KeyPair struct {
	AuthenticationKey []byte
	EncryptionKey     []byte
}

KeyPair rappresent two keys, an AuthenticationKey used for signing and an optional (can be nil) EncryptionKey for encryption

type MySqlStoreError

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

MySqlStoreError provides a custom wrapper for encapsulating the function name with an error

func (MySqlStoreError) Error

func (e MySqlStoreError) Error() string

Error Implement the Error interface, it returns a string like this:

funcName: errorMessage

func (MySqlStoreError) Unwrap

func (e MySqlStoreError) Unwrap() error

Unwrap implements the Unwrap interface, used to unwrap error after an errors.Join

type SqlxStore

type SqlxStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options // default configuration
	// contains filtered or unexported fields
}

SqlxStore provides session data from a mysql database

func NewSqlxStore

func NewSqlxStore(db *sqlx.DB, tableName string, keys []KeyPair, opts ...SqlxStoreOption) (*SqlxStore, error)

NewSqlxStore creates a new SqlxStore that can be used to retrieve and save user sessions from a database. IMPORTANT: When passing the database connection make sure to have the option parseTime set to true, otherwise you may have problems.

func NewSqlxStoreFromDsn

func NewSqlxStoreFromDsn(dsn string, tableName string, keys []KeyPair, opts ...SqlxStoreOption) (*SqlxStore, error)

NewSqlxStoreFromDsn creates a new SqlxStore that can be used to retrieve and save user sessions from a database, it uses a dsn string to create a connection to the database. IMPORTANT: The dsn string should contain the parameter parseTime=true otherwise you may have problems.

func (*SqlxStore) Close

func (store *SqlxStore) Close()

Close stop the cleanup goroutine and closes the database connection

func (*SqlxStore) Delete

func (store *SqlxStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session)

Delete change the session options in order that it will be deleted when calling [Save]

func (*SqlxStore) Get

func (store *SqlxStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get should return a cached session. Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded or is expired.

func (*SqlxStore) New

func (store *SqlxStore) New(r *http.Request, name string) (*sessions.Session, error)

New should create and return a new session.

Note that New should never return a nil session, even in the case of an error if using the Registry infrastructure to cache the session. New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call

func (*SqlxStore) Save

func (store *SqlxStore) Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error

Save stores the session data in the database

func (*SqlxStore) SetMaxAge

func (store *SqlxStore) SetMaxAge(maxAge int)

SetMaxAge sets the maxAge parameter for the cookie

type SqlxStoreOption

type SqlxStoreOption func(*SqlxStore)

SqlxStoreOption A list of SqlxStoreOption can be used when creating a SqlxStore when using NewSqlxStore or NewSqlxStoreFromDsn

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) SqlxStoreOption

WithCleanupInterval returns a SqlxStoreOption that allows to enable and set the cleanup interval, the cleanup interval is the time between each scan to remove expired sessions from the database

func WithDomain

func WithDomain(domain string) SqlxStoreOption

WithDomain returns a SqlxStoreOption that allows to change the domain parameter of the session cookie that is provided by default when creating a new session

func WithHttpOnly

func WithHttpOnly(httpOnly bool) SqlxStoreOption

WithHttpOnly returns a SqlxStoreOption that allows to change the httpOnly parameter of the session cookie that is provided by default when creating a new session

func WithMaxAge

func WithMaxAge(maxAge int) SqlxStoreOption

WithMaxAge returns a SqlxStoreOption that allows to change the maxAge parameter of the session cookie that is provided by default when creating a new session MaxAge=0 means no Max-Age attribute specified and the cookie will be deleted after the browser session ends. MaxAge<0 means delete cookie immediately. MaxAge>0 means Max-Age attribute present and given in seconds.

func WithPartitioned

func WithPartitioned(isPartitioned bool) SqlxStoreOption

WithPartitioned returns a SqlxStoreOption that allows to enable or disable the Partitioned attribute in a cookie

func WithPath

func WithPath(path string) SqlxStoreOption

WithPath returns a SqlxStoreOption that allows to change the path parameter of the session cookie that is provided by default when creating a new session

func WithSameSite

func WithSameSite(sameSite http.SameSite) SqlxStoreOption

WithSameSite returns a SqlxStoreOption that allows to change the sameSite parameter of the session cookie that is provided by default when creating a new session

func WithSecure

func WithSecure(secure bool) SqlxStoreOption

WithSecure returns a SqlxStoreOption that allows to change the secure parameter of the session cookie that is provided by default when creating a new session

Jump to

Keyboard shortcuts

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