passwordreset

package
v0.0.0-...-3a75ae7 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldNameEmail      = "email"
	FieldNameResetToken = "reset_token"
	FieldNamePassword   = "password"
)
View Source
const (
	ResetTokenExpiry = time.Duration(12 * time.Hour)
	ResetTokenLength = 32
)

Variables

View Source
var (
	// ErrUsedPasswordResetToken is returned when the password reset token has already been used.
	ErrUsedPasswordResetToken = errors.New("password/reset: used password reset token")
)

Functions

func WithConfig

func WithConfig(config *Config) func(*FormConfig)

WithConfig sets the configuration for the underlying Handler for FormHandler.

func WithLogger

func WithLogger(logger *slog.Logger) func(*Config)

WithLogger configures the logger.

func WithPasswordHasher

func WithPasswordHasher(hasher password.PasswordHasher) func(*Config)

WithPasswordHasher configures the password hasher.

When setting a password hasher make sure to set it across all modules, such as user registrration, password reset and password verification.

Types

type Config

type Config struct {
	PasswordHasher password.PasswordHasher // optional
	Logger         *slog.Logger            // optional

	// TokenLength set the length of the reset token.
	//
	// Defaults to ResetTokenExpiry.
	TokenLength int // optinal
	// TokenExpiryIn set the expiry time of the reset token.
	TokenExpiryIn time.Duration // optional
}

Config is the configuration for the password handler.

Make sure to use the NewConfig function to create a new config, instead of instatiating the struct directly.

func NewConfig

func NewConfig(config ...func(*Config)) *Config

NewConfig creates a new config.

type FormConfig

type FormConfig struct {
	*Config

	EmailFieldName      string
	ResetTokenFieldName string
	PasswordFieldName   string
}

FormConfig is the configuration for form-based password reset.

func NewFormConfig

func NewFormConfig(
	config ...func(*FormConfig),
) *FormConfig

NewFormConfig creates a new FormConfig with the given configuration options.

type FormHandler

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

FormHandler is a wrapper around Handler handling HTTP form requests.

func NewFormHandler

func NewFormHandler(
	driver driver.Driver,
	sender sender.Sender,
	config *FormConfig,
) *FormHandler

NewFormHandler creates a new FormHandler with the given configuration.

func (*FormHandler) HandlePasswordReset

func (h *FormHandler) HandlePasswordReset(req *http.Request) error

HandlePasswordReset handles a password reset request.

func (*FormHandler) HandlePasswordResetConfirm

func (h *FormHandler) HandlePasswordResetConfirm(req *http.Request) error

HandlePasswordResetConfirm handles a password reset confirmation.

type Handler

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

Handler handles password reset requests.

It is a general enough implementation so it can be used for different communication methods.

Check out the FormHandler for a ready to use implementation that handles HTTP form requests.

func (*Handler) HandlePasswordReset

func (h *Handler) HandlePasswordReset(
	ctx context.Context,
	email string,
) error

HandlePasswordReset handles a password reset request.

func (*Handler) HandlePasswordResetConfirm

func (h *Handler) HandlePasswordResetConfirm(
	ctx context.Context,
	password, tokStr string,
) error

type PasswordResetRequestMessagePayload

type PasswordResetRequestMessagePayload struct {
	Token string
}

ResetTokenMessagePayload is the payload for the reset token message.

type PasswordResetSuccessMessagePayload

type PasswordResetSuccessMessagePayload struct{}

PasswordResetSuccessMessagePayload is the payload for the password reset success message.

Jump to

Keyboard shortcuts

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