models

package
v0.0.0-...-c9dde97 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultResetDuration = 1 * time.Hour
)
View Source
const (
	DefaultSender = "support@lenslocked.com"
)
View Source
const (
	// The minimum bytes per session token.
	MinBytesPerToken = 32
)

Variables

View Source
var (
	ErrAuthFailed = errors.New("models: authentication failed.")
	ErrEmailTaken = errors.New("models: email address is already in use")
	ErrNotFound   = errors.New("models: resource could not be found")
)
View Source
var (
	ErrUserNotFound = errors.New("models: user not found.")
	ErrTokenInvalid = errors.New("models: token invalid.")
	ErrTokenExpired = errors.New("models: token expired.")
)

Functions

func Migrate

func Migrate(db *sql.DB, dir string) error

func MigrateFS

func MigrateFS(db *sql.DB, migrationsFS fs.FS, dir string) error

func Open

func Open(cfg PostgresConfig) (*sql.DB, error)

Open will open a SQL connection with the provided PostgresConfig. Callers of Open need to ensure that the connection is eventually closed via db.Close() method.

Types

type Email

type Email struct {
	From      string
	To        string
	Subject   string
	Plaintext string
	HTML      string
}

type EmailService

type EmailService struct {
	// DefaultSender is used as the default sender when one isn't provided for an
	// email. This is also used in functions where the email is a predetermined,
	// like the forgotten password email.
	DefaultSender string
	// contains filtered or unexported fields
}

func NewEmailService

func NewEmailService(config SMTPConfig) *EmailService

func (*EmailService) ForgotPassword

func (es *EmailService) ForgotPassword(to, resetURL string) error

func (*EmailService) Send

func (es *EmailService) Send(email Email) error
type Gallery struct {
	ID     int
	UserID int
	Title  string
}

type GalleryService

type GalleryService struct {
	DB *sql.DB
}

func (*GalleryService) ByID

func (srv *GalleryService) ByID(id int) (*Gallery, error)

func (*GalleryService) ByUserID

func (srv *GalleryService) ByUserID(userID int) ([]Gallery, error)

func (*GalleryService) Create

func (srv *GalleryService) Create(title string, userID int) (*Gallery, error)

func (*GalleryService) Delete

func (srv *GalleryService) Delete(id int) error

func (*GalleryService) Update

func (srv *GalleryService) Update(gallery *Gallery) error

type PasswordReset

type PasswordReset struct {
	ID     int
	UserID int
	// Token is only set when creating a new Password Reset Token. When looking up a Token
	// this will be left empty, as we only store the hash of the reset token in the
	// database.
	Token     string
	TokenHash string
	ExpiresAt time.Time
}

type PasswordResetService

type PasswordResetService struct {
	DB *sql.DB
	// BytesPerToken is used to determin how many bytes to use when generating each
	// PasswordReset token. If this value is not set or is less than the MinBytesPerToken
	// const it will be ignored and MinBytesPerToken will be used.
	BytesPerToken int
	// Duration is the amount of time that a PasswordReset token is valid for.
	// Defaults to DefaultResetDuration
	Duration time.Duration
}

func (*PasswordResetService) Consume

func (service *PasswordResetService) Consume(token string) (*User, error)

func (*PasswordResetService) Create

func (service *PasswordResetService) Create(email string) (*PasswordReset, error)

type PostgresConfig

type PostgresConfig struct {
	Host     string
	Port     string
	User     string
	Password string
	Database string
	SSLMode  string
}

func DefaultPostgresConfig

func DefaultPostgresConfig() PostgresConfig

func (PostgresConfig) String

func (cfg PostgresConfig) String() string

type SMTPConfig

type SMTPConfig struct {
	Host     string
	Port     int
	Username string
	Password string
}

type Session

type Session struct {
	ID     int
	UserID int
	// Token is only set when creating a new session, looking up a will leave this
	// field empty, as we only store a token hash.
	Token     string
	TokenHash string
}

type SessionService

type SessionService struct {
	DB *sql.DB
	// BytesPerToken is used to determine how many bytes to use when generating
	// each session token. If this value is not set or is less than the MinBytesPerToken
	// const it will be ignored and MinBytesPerToken will be used.
	BytesPerToken int
}

func (*SessionService) Create

func (ss *SessionService) Create(userID int) (*Session, error)

func (*SessionService) Delete

func (ss *SessionService) Delete(token string) error

func (*SessionService) User

func (ss *SessionService) User(token string) (*User, error)

type User

type User struct {
	ID           int
	Email        string
	PasswordHash string
}

type UserService

type UserService struct {
	DB *sql.DB
}

func (*UserService) Authenticate

func (us *UserService) Authenticate(email, password string) (*User, error)

func (*UserService) Create

func (us *UserService) Create(email, password string) (*User, error)

func (*UserService) UpdatePassword

func (us *UserService) UpdatePassword(userID int, password string) error

Jump to

Keyboard shortcuts

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