utils

package
v0.0.0-...-73db963 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("tokens is invalid")
	ErrExpiredToken = errors.New("tokens has expired")
)

Different types of error returned by the VerifyToken function

Functions

func CheckPassword

func CheckPassword(password string, hashedPassword string) error

CheckPassword checks if the provided password is correct or not

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns the bcrypt hash of the password

func Logger

func Logger() gin.HandlerFunc

func RandomInt

func RandomInt(min, max int64) int64

RandomInt generates a random integer between min and max

func RandomString

func RandomString(n int) string

RandomString generates a random string of length n

func SortBy

func SortBy(orderBy string, query *gorm.DB) *gorm.DB

func Transaction

func Transaction(db *gorm.DB, f func(tx *gorm.DB) error) error

Types

type JWTMaker

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

JWTMaker is a JSON Web Token maker

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(userId int, duration time.Duration) (string, *TokenPayload, error)

CreateToken creates a new tokens for a specific username and duration

func (*JWTMaker) VerifyToken

func (maker *JWTMaker) VerifyToken(token string) (*TokenPayload, error)

VerifyToken checks if the tokens is valid or not

type Pagination

type Pagination struct {
	TotalItems int64 `json:"total_items"`
	TotalPages int   `json:"total_pages"`
	Page       int   `json:"page"`
	Limit      int   `json:"limit"`
	HasPrev    bool  `json:"has_prev"`
	HasNext    bool  `json:"has_next"`
}

func Paginate

func Paginate(count int64, page int, limit int) *Pagination

type Response

type Response struct {
	Status  ResponseStatus `json:"status"`
	Message string         `json:"message"`
	Data    any            `json:"data"`
}

func ResponseData

func ResponseData(status ResponseStatus, message string, data any) *Response

type ResponseStatus

type ResponseStatus string
const (
	ResponseStatusSuccess ResponseStatus = "success"
	ResponseStatusError   ResponseStatus = "error"
)

type TokenMaker

type TokenMaker interface {
	// CreateToken creates a new tokens for a specific username and duration
	CreateToken(userId int, duration time.Duration) (string, *TokenPayload, error)

	// VerifyToken checks if the tokens is valid or not
	VerifyToken(token string) (*TokenPayload, error)
}

TokenMaker is an interface for managing tokens

func NewJWTMaker

func NewJWTMaker(secretKey string) (TokenMaker, error)

type TokenPayload

type TokenPayload struct {
	Id        uuid.UUID `json:"id"`
	UserId    int       `json:"user_id"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

TokenPayload contains the payload data of the tokens

func NewPayload

func NewPayload(userId int, duration time.Duration) (*TokenPayload, error)

NewPayload creates a new tokens payload with a specific username and duration

func (*TokenPayload) Valid

func (payload *TokenPayload) Valid() error

Valid checks if the tokens payload is valid or not

Jump to

Keyboard shortcuts

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