axth

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

github.com/axiangcoding/axth

[Not Release Yet] A set of tool to make login and register easy!

codecov CodeQL codebeat badge Test Total alerts

Documentation

Index

Constants

View Source
const (
	// UserStatusNormal user status is normal
	UserStatusNormal = "normal"
	// UserStatusBanned user status is banned
	UserStatusBanned = "banned"
)
View Source
const (
	FieldId     = "id"
	FieldEmail  = "email"
	FieldPhone  = "phone"
	FieldUserId = "userId"
)

Variables

View Source
var (
	ErrInternalFailed         = errors.New("axth internal failed")
	ErrUserNotExist           = errors.New("user not exist")
	ErrUserPasswordNotMatched = errors.New("user password not matched")
	ErrUserUpdateFailed       = errors.New("user profile update failed")
)

Functions

func CheckOptions

func CheckOptions(opt Options) error

Types

type AxthUser

type AxthUser struct {
	gorm.Model
	UserID           string `gorm:"uniqueIndex;size:255"`
	DisplayName      string `gorm:"size:255"`
	AvatarUrl        string `gorm:"size:255"`
	Email            string `gorm:"uniqueIndex;size:255"`
	Phone            string `gorm:"uniqueIndex;size:255"`
	Password         string `gorm:"size:255"`
	Status           string `gorm:"size:255"`
	LoginFailedCount int
	LastLoginTime    time.Time
}

AxthUser user schema

func (AxthUser) ToDisplayUser

func (r AxthUser) ToDisplayUser() *DisplayUser

type DisplayUser

type DisplayUser struct {
	ID               uint      `json:"id"`
	UserID           string    `json:"user_id"`
	DisplayName      string    `json:"display_name"`
	AvatarUrl        string    `json:"avatar_url"`
	Email            string    `json:"email"`
	Phone            string    `json:"phone"`
	Status           string    `json:"status"`
	LoginFailedCount int       `json:"login_failed_count"`
	CreatedAt        time.Time `json:"created_at"`
	UpdatedAt        time.Time `json:"updated_at"`
	LastLoginTime    time.Time `json:"last_login_time"`
}

DisplayUser For user display

type Enforcer

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

func NewEnforcer

func NewEnforcer(db *gorm.DB, opt *Options) (*Enforcer, error)

NewEnforcer create a new enforcer

func (*Enforcer) CheckEmailExist

func (e *Enforcer) CheckEmailExist(email string) (bool, error)

CheckEmailExist check if email already exist

func (*Enforcer) CheckPhoneExist

func (e *Enforcer) CheckPhoneExist(phone string) (bool, error)

CheckPhoneExist check if phone already exist

func (*Enforcer) CheckUserIdExist

func (e *Enforcer) CheckUserIdExist(userId string) (bool, error)

CheckUserIdExist check if userId already exist

func (*Enforcer) FindUser

func (e *Enforcer) FindUser(userId string) (*DisplayUser, error)

FindUser find a user

func (*Enforcer) Login

func (e *Enforcer) Login(userId string, password string) (*DisplayUser, error)

Login user login with userId and password, default login method

func (*Enforcer) LoginWithEmail

func (e *Enforcer) LoginWithEmail(email string, password string) (*DisplayUser, error)

LoginWithEmail user login with email

func (*Enforcer) LoginWithPhone

func (e *Enforcer) LoginWithPhone(phone string, password string) (*DisplayUser, error)

LoginWithPhone user login with phone

func (*Enforcer) Register

func (e *Enforcer) Register(ru RegisterUser) (bool, error)

Register register a user

func (*Enforcer) ResetPassword

func (e *Enforcer) ResetPassword(userId string, oldPwd string, newPwd string) (bool, error)

ResetPassword reset account password

func (*Enforcer) UpdateUser

func (e *Enforcer) UpdateUser(userId string, user AxthUser) (bool, error)

UpdateUser update user by userId

type IEnforcer

type IEnforcer interface {
	Login(userId string, password string) (*DisplayUser, error)
	LoginWithEmail(email string, password string) (*DisplayUser, error)
	LoginWithPhone(phone string, password string) (*DisplayUser, error)
	ResetPassword(userId string, oldPwd string, newPwd string) (bool, error)
	Register(ru RegisterUser) (bool, error)
	UpdateUser(userId string, user AxthUser) (bool, error)
	FindUser(userId string) (*DisplayUser, error)
	CheckUserIdExist(userId string) (bool, error)
	CheckEmailExist(email string) (bool, error)
	CheckPhoneExist(phone string) (bool, error)
}

type Options

type Options struct {
	// auto migrate inner table
	DbAutoMigrate bool
	// user max login failed times
	UserMaxLoginFailed int `validate:"gte=0"`
	// if user reach max login failed, wait for duration to unlock
	UserLoginFailedUnlockDuration time.Duration
}

func DefaultOptions

func DefaultOptions() (*Options, error)

type RegisterUser

type RegisterUser struct {
	UserID      string `json:"user_id,omitempty"`
	DisplayName string `json:"display_name,omitempty"`
	AvatarUrl   string `json:"avatar_url,omitempty"`
	Email       string `json:"email,omitempty"`
	Phone       string `json:"phone,omitempty"`
	Password    string `json:"password,omitempty"`
}

RegisterUser For user register

func (RegisterUser) ToAxUser

func (u RegisterUser) ToAxUser() *AxthUser

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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