users

package module
v0.0.0-...-798843e Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

go-users

Simple implementation of users interaction

Installation

go get -u github.com/dmitrymomot/go-users

Usage

package main

import (
    "github.com/dmitrymomot/go-users"
    "github.com/jmoiron/sqlx"
    _ "github.com/mattn/go-sqlite3" // or another driver
)

func main() {
    // DB connection
    db, err := sqlx.Connect("sqlite3", ":memory:")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Init user repository
    // Table `users` must be exists in database
    ur := users.NewRepository(db, "users")
    // Init user interactor
    ui := users.NewInteractor(ur, "your secret signing key")

    // your code ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound              = errors.New("user not found")
	ErrInvalidPassword       = errors.New("invalid password")
	ErrTakenEmail            = errors.New("email is already taken")
	ErrEmailMissed           = errors.New("email is missed")
	ErrNotExistedUser        = errors.New("could not update not existed user")
	ErrCouldNotSetPassword   = errors.New("could not set password")
	ErrInvalidToken          = errors.New("invalid token string")
	ErrCouldNotGenerateToken = errors.New("could not generate new token")
)

Predefined users package errors

Functions

This section is empty.

Types

type Condition

type Condition interface {
	Query() string
	Params() []interface{}
	Type() QuerySection
}

Condition struct

func Confirmed

func Confirmed(v bool) Condition

Confirmed func add condition to select items only with confirmed=v

func Disabled

func Disabled(v bool) Condition

Disabled func add condition to select items only with disabled=v

func Limit

func Limit(v int) Condition

Limit func add limit to select query

func Offset

func Offset(v int) Condition

Offset func add offset to select query

func OrderBy

func OrderBy(order ...Order) Condition

OrderBy func add ordering to selected list

type Interactor

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

Interactor structure

func New

func New(db *sql.DB, driverName, tableName, signingKey string) *Interactor

New function is a factory which returns users Interactor instance with injected users repository Can be used as a helper to make the code shorter

func NewInteractor

func NewInteractor(r UserRepository, signingKey string) *Interactor

NewInteractor factory

func (*Interactor) Confirm

func (i *Interactor) Confirm(token string) error

Confirm function checks token and set confirmed=true if it's valid

func (*Interactor) ConfirmationToken

func (i *Interactor) ConfirmationToken(u *User, ttl int64) (string, error)

ConfirmationToken returns confirmation token string

func (*Interactor) Create

func (i *Interactor) Create(u *User) error

Create new user

func (*Interactor) Delete

func (i *Interactor) Delete(id string) error

Delete user by id

func (*Interactor) GetByEmail

func (i *Interactor) GetByEmail(email string) (*User, error)

GetByEmail fetch user by email

func (*Interactor) GetByID

func (i *Interactor) GetByID(id string) (*User, error)

GetByID fetch user by id

func (*Interactor) GetList

func (i *Interactor) GetList(c ...Condition) ([]*User, error)

GetList od users with sorting and optional conditional

func (*Interactor) ResetPassword

func (i *Interactor) ResetPassword(token string, newPassword string) error

ResetPassword fucntion validates token and updates password

func (*Interactor) ResetPasswordToken

func (i *Interactor) ResetPasswordToken(u *User, ttl int64) (string, error)

ResetPasswordToken returns reset password token string

func (*Interactor) Update

func (i *Interactor) Update(u *User) error

Update existed user

func (*Interactor) VerifyPassword

func (i *Interactor) VerifyPassword(u *User, passwordStr string) error

VerifyPassword compare password string with user password hash, returns nil if password is valid

type Order

type Order int

Order type

const (
	// ORDER BY created_at ASC
	OrderByCreatedAtAsc Order = iota + 1
	// ORDER BY created_at DESC
	OrderByCreatedAtDesc
	// ORDER BY updated_at ASC
	OrderByUpdatedAtAsc
	// ORDER BY updated_at DESC
	OrderByUpdatedAtDesc
)

Predefined users list ordering

func (Order) String

func (o Order) String() string

type QuerySection

type QuerySection int

QuerySection type

const (
	QsWhere QuerySection = iota + 1
	QsOrderBy
	QsLimit
	QsOffset
)

Predefined query section type

type Repository

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

Repository structure is implementation of UserRepository interface

func NewRepository

func NewRepository(db *sql.DB, driverName, tableName string) *Repository

NewRepository factory

func (*Repository) Delete

func (r *Repository) Delete(id string) error

Delete a user record by id

func (*Repository) GetByEmail

func (r *Repository) GetByEmail(email string) (*User, error)

GetByEmail fetch user record by email

func (*Repository) GetByID

func (r *Repository) GetByID(id string) (*User, error)

GetByID fetch user record by id

func (*Repository) GetList

func (r *Repository) GetList(c ...Condition) ([]*User, error)

GetList fetch users list

func (*Repository) Insert

func (r *Repository) Insert(u *User) error

Insert a new user record

func (*Repository) Update

func (r *Repository) Update(u *User) error

Update existed user record

type User

type User struct {
	ID        string `db:"id" json:"id"`
	Email     string `db:"email" json:"email"`
	Password  string `db:"password" json:"-"`
	Confirmed bool   `db:"confirmed" json:"confirmed"`
	Disabled  bool   `db:"disabled" json:"disabled"`
	CreatedAt int64  `db:"created_at" json:"created_at"`
	UpdatedAt *int64 `db:"updated_at" json:"updated_at,omitempty,"`
}

User model structure

func (*User) SetPassword

func (u *User) SetPassword(password string) error

SetPassword helper

type UserRepository

type UserRepository interface {
	GetByID(id string) (*User, error)
	GetByEmail(email string) (*User, error)
	GetList(...Condition) ([]*User, error)
	Insert(*User) error
	Update(*User) error
	Delete(id string) error
}

UserRepository interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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