Documentation ¶
Overview ¶
Package password implements a user registration and login flows with password.
Index ¶
Constants ¶
const ( DefaultFieldNameFirstName = "first_name" DefaultFieldNameLastName = "last_name" DefaultFieldNameEmail = "email" DefaultFieldNamePassword = "password" )
const (
BcryptDefaultCost = bcrypt.DefaultCost
)
Variables ¶
var ( FormValidator = validator.New(validator.WithRequiredStructEnabled()) FormScrubber = scrubbers.New() FormModifier = modifiers.New() )
var ( ErrEmailAlreadyTaken = fmt.Errorf("shield/password: email already taken") ErrPasswordIncorrect = fmt.Errorf("shield/password: password incorrect") )
var DefaultPasswordHasher = NewBcryptPasswordHasher(BcryptDefaultCost)
DefaultPasswordHasher is the default password hashing algorithm used across.
Functions ¶
func WithHijacker ¶
func WithPasswordHasher ¶
func WithPasswordHasher[T any](hasher PasswordHasher) func(*Config[T])
WithPasswordHasher configures the password hasher.
When setting a password hasher make sure to set it across all modules, i.e., user registration, password reset and password verification.
Types ¶
type Config ¶
type Config[T any] struct { Logger *slog.Logger PasswordHasher PasswordHasher PasswordVerifier shieldpasswordverifier.PasswordVerifier Hijacker Hijacker[T] }
Config is the configuration for the password handler.
type FormConfig ¶
type FormConfig[T any] struct { *Config[T] FirstNameFieldName string // optional (default: DefaultFieldNameFirstName) LastNameFieldName string // optional (default: DefaultFieldNameLastName) EmailFieldName string // optional (default: DefaultFieldNameEmail) PasswordFieldName string // optional (default: DefaultFieldNamePassword) }
func NewFormConfig ¶
func NewFormConfig[T any](opts ...func(*FormConfig[T])) *FormConfig[T]
NewFormConfig[T] creates a new FormConfig[T] with the given configuration options.
type FormHandler ¶
type FormHandler[T any] struct { // contains filtered or unexported fields }
FormHandler[T] is a wrapper around Handler handling HTTP form requests.
func NewFormHandler ¶
func NewFormHandler[T any](pool *pgxpool.Pool, config *FormConfig[T]) *FormHandler[T]
NewFormHandler[T] creates a new FormHandler[T] with the given configuration.
If config is nil, the default config is used.
func (*FormHandler[T]) HandleUserLogin ¶
HandleUserLogin handles a user login request.
func (*FormHandler[T]) HandleUserRegistration ¶
HandleUserRegistration handles a user registration request.
type Handler ¶
type Handler[T any] struct { // contains filtered or unexported fields }
func (*Handler[T]) HandleUserLogin ¶
type Hijacker ¶
type Hijacker[T any] interface { // HijackUserRegisteration is called when registring a new user. // Use this method to create an additional context for the user. HijackUserRegisteration(context.Context, uuid.UUID, pgx.Tx) (T, error) // HijackUserLogin is called when a user is trying to login. // Use this method to fetch additional data from the database for the user. // // Note that the user password is not verified at this moment yet. HijackUserLogin(context.Context, uuid.UUID, pgx.Tx) (T, error) }
Hijacker also to hijack into the user registration and logging in sessions and perform additional operations.
type PasswordHasher ¶
type PasswordHasher interface { Hash(password string) (string, error) Verify(hashedPassword string, password string) (bool, error) }
PasswordHasher is a hashing algorithm to hash password securely.
func NewBcryptPasswordHasher ¶
func NewBcryptPasswordHasher(cost int) PasswordHasher
NewBcryptPasswordHasher implements a password hashing algorithm with bcrypt.