session

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: MIT Imports: 8 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// Default Flash Type
	FlashError   = "error"
	FlashInfo    = "info"
	FlashSuccess = "success"
	FlashWarning = "warning"

	// Default Flash Msg
	BadCredsMsg      = "Hmm... check those credentials."
	BadInputMsg      = "Hmm... check your form, something isn't correct."
	DefaultErrMsg    = "Uh oh! We've run into an issue."
	EmailNotValidMsg = "It looks like your primary email has not been validated. Please complete this process and try again."
	LinkSentMsg      = "Email sent! Please open the link in your email to reset your password."
	NoAccessMsg      = "Oops, sending you back somewhere safe."
)

Variables

View Source
var (
	ErrNotValid = errors.New("not valid")
	ErrNoUser   = errors.New("no user")
)
View Source
var ContactUsErr = DefaultErrMsg + " Please contact us at %s if the issue persists."

Functions

This section is empty.

Types

type Config added in v0.6.1

type Config struct {
	Env trails.Environment

	// The name sessions are stored under.
	// Also used as the name of the cookie when WithCookie is used.
	SessionName string

	// Hex-encoded key
	AuthKey string

	// Hex-encoded key
	EncryptKey string
}

A Config provides the required values

type Flash

type Flash struct {
	Type string `json:"type"`
	Msg  string `json:"message"`
}

A Flash is a structured message set in a session.

func (Flash) GetClass added in v0.3.9

func (f Flash) GetClass() string

type Service

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

A Service wraps a gorilla.Store to manage constructing a new one and accessing the sessions contained in it.

Service implements SessionStorer.

func NewStoreService

func NewStoreService(cfg Config, opts ...ServiceOpt) (Service, error)

NewStoreService initiates a data store for user web sessions with the provided config. If no backing storage is provided through a functional option - like WithRedis - NewService stores sessions in cookies.

func (Service) GetSession

func (s Service) GetSession(r *http.Request) (Session, error)

GetSession retrieves the Session for the *http.Request, or creates a brand new one.

type ServiceOpt

type ServiceOpt func(*Service) error

A ServiceOpt configures the provided *Service, returning an error if unable to.

func WithCookie

func WithCookie() ServiceOpt

WithCookie configures the Service to back session storage with cookies.

func WithMaxAge

func WithMaxAge(secs int) ServiceOpt

WithMaxAge sets the time-to-live of a session.

Call before other options so this value is available.

Otherwise, the Service uses defaultMaxAge.

func WithRedis

func WithRedis(uri, pass string) ServiceOpt

WithRedis configures the Service to back session storage with Redis.

To authenticate to the Redis server, provide pass, otherwise its zero-value is acceptable.

type Session

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

A Session provides all functionality for managing a fully featured session.

Its functionality is implemented by lightly wrapping a gorilla.Session.

func (Session) ClearFlashes

func (s Session) ClearFlashes(w http.ResponseWriter, r *http.Request)

ClearFlashes removes all Flashes from the Session.

func (Session) Delete

func (s Session) Delete(w http.ResponseWriter, r *http.Request) error

Delete removes a session by making the MaxAge negative.

func (Session) DeregisterUser

func (s Session) DeregisterUser(w http.ResponseWriter, r *http.Request) error

DeregisterUser removes the User from the session.

func (Session) Flashes

func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []Flash

Flashes retrieves []Flash stored in the session.

func (Session) Get

func (s Session) Get(key string) any

Get retrieves a value from the session according to the key passed in.

func (Session) RegisterUser

func (s Session) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error

RegisterUserSession stores the user's ID in the session.

func (Session) ResetExpiry

func (s Session) ResetExpiry(w http.ResponseWriter, r *http.Request) error

ResetExpiry resets the expiration of the session by saving it.

func (Session) Save

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

Save wraps gorilla.Session.Save, saving the session in the request.

func (Session) Set

func (s Session) Set(w http.ResponseWriter, r *http.Request, key trails.Key, val any) error

Set stores a value according to the key passed in on the session.

func (Session) SetFlash

func (s Session) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error

SetFlash stores the passed in Flash in the session.

func (Session) UserID

func (s Session) UserID() (uint, error)

UserID gets the user ID out of the session. A user ID should be present in a session if the user is successfully authenticated. If no user ID can be found, this ErrNoUser is returned. This ought to only happen when a user is going through an authentication workflow or hitting unauthenticated pages.

If the value returned from the session is not a uint, ErrNotValid is returned and represents a programming error.

type SessionStorer

type SessionStorer interface {
	GetSession(r *http.Request) (Session, error)
}

The SessionStorer defines methods for interacting with a Sessionable for the given *http.Request.

type Stub

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

func NewStub added in v0.6.1

func NewStub(loggedIn bool) *Stub

func (Stub) Get

func (s Stub) Get(r *http.Request, name string) (*gorilla.Session, error)

func (*Stub) GetSession added in v0.6.1

func (s *Stub) GetSession(r *http.Request) (Session, error)

func (Stub) New added in v0.6.1

func (s Stub) New(r *http.Request, name string) (*gorilla.Session, error)

func (Stub) Save

func (s Stub) Save(r *http.Request, w http.ResponseWriter, sess *gorilla.Session) error

Jump to

Keyboard shortcuts

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