Documentation ¶
Index ¶
- Constants
- Variables
- func WithCookie() func(*Service) error
- func WithMaxAge(secs int) func(*Service) error
- func WithRedis(uri, pass string) func(*Service) error
- type Flash
- type FlashSessionable
- type Service
- type ServiceOpt
- type Session
- func (s Session) ClearFlashes(w http.ResponseWriter, r *http.Request)
- func (s Session) Delete(w http.ResponseWriter, r *http.Request) error
- func (s Session) DeregisterUser(w http.ResponseWriter, r *http.Request) error
- func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []Flash
- func (s Session) Get(key string) interface{}
- func (s Session) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error
- func (s Session) ResetExpiry(w http.ResponseWriter, r *http.Request) error
- func (s Session) Save(w http.ResponseWriter, r *http.Request) error
- func (s Session) Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error
- func (s Session) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error
- func (s Session) UserID() (uint, error)
- type SessionStorer
- type Sessionable
- type Stub
- func (s Stub) ClearFlashes(w http.ResponseWriter, r *http.Request)
- func (s Stub) Delete(w http.ResponseWriter, r *http.Request) error
- func (s Stub) DeregisterUser(w http.ResponseWriter, r *http.Request) error
- func (s Stub) Flashes(w http.ResponseWriter, r *http.Request) []Flash
- func (s Stub) Get(key string) interface{}
- func (s Stub) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error
- func (s Stub) ResetExpiry(w http.ResponseWriter, r *http.Request) error
- func (s Stub) Save(w http.ResponseWriter, r *http.Request) error
- func (s Stub) Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error
- func (s Stub) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error
- func (s Stub) UserID() (uint, error)
- type TrailsSessionable
- type UserSessionable
Constants ¶
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 ¶
var ( ErrFailedConfig = errors.New("failed config") ErrNotValid = errors.New("not valid") ErrNoUser = errors.New("no user") )
var ContactUsErr = DefaultErrMsg + " Please contact us at %s if the issue persists."
Functions ¶
func WithCookie ¶
WithCookie configures the Service to back session storage with cookies.
func WithMaxAge ¶
WithMaxAge sets the time-to-live of a session.
Call before other options so this value is available.
Otherwise, the Service uses defaultMaxAge.
Types ¶
type FlashSessionable ¶
type FlashSessionable interface { ClearFlashes(w http.ResponseWriter, r *http.Request) Flashes(w http.ResponseWriter, r *http.Request) []Flash SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error }
FlashSessionable defines the behavior of a session that includes flashes in it.
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(env, authKey, encryptKey string, opts ...ServiceOpt) (Service, error)
NewStoreService initiates a data store for user web sessions with the provided hex-encoded authentication key and encryption keys. 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) (Sessionable, error)
GetSession wraps gorilla.Get, creating a brand new Session or one from the session retrieved.
type ServiceOpt ¶
A ServiceOpt configures the provided *Service, returning an error if unable to.
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.
TODO(dlk): embed *gorilla.Session anonymously? do not export?
func (Session) ClearFlashes ¶
func (s Session) ClearFlashes(w http.ResponseWriter, r *http.Request)
func (Session) DeregisterUser ¶
DeregisterUser removes the User from the session.
func (Session) RegisterUser ¶
RegisterUserSession stores the user's ID in the session.
func (Session) ResetExpiry ¶
ResetExpiry resets the expiration of the session by saving it.
func (Session) UserID ¶
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) (Sessionable, error) // TODO(dlk): Sessionable or TrailsSessionable?
}
The SessionStorer defines methods for interacting with a Sessionable for the given *http.Request.
type Sessionable ¶
type Sessionable interface { Delete(w http.ResponseWriter, r *http.Request) error Get(key string) interface{} ResetExpiry(w http.ResponseWriter, r *http.Request) error Save(w http.ResponseWriter, r *http.Request) error Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error }
The Sessionable wraps methods for basic adding values to, deleting, and getting values from a session associated with an *http.Request and saving those to the session store.
type Stub ¶
type Stub struct{}
func (Stub) ClearFlashes ¶
func (s Stub) ClearFlashes(w http.ResponseWriter, r *http.Request)
func (Stub) DeregisterUser ¶
func (Stub) RegisterUser ¶
func (Stub) ResetExpiry ¶
type TrailsSessionable ¶
type TrailsSessionable interface { FlashSessionable Sessionable UserSessionable }
The TrailsSessionable composes session's major interfaces.
func NewSession ¶
func NewSession(g *gorilla.Session) TrailsSessionable
NewSession constructs a new Session as an implementation of TrailsSessionable from an interface value that is a *gorilla.Session. If it isn't, ErrNotValid returns.
Typical usage is to pass in the value retrieved from a http.Request.Context. Given context keys are unexported, this package cannot perform that retrieval.
type UserSessionable ¶
type UserSessionable interface { DeregisterUser(w http.ResponseWriter, r *http.Request) error RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error UserID() (uint, error) }
The UserSessionable wraps methods for adding, removing, and retrieving user IDs from a session.