utils

package
v0.0.0-...-04ded95 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, message string)

func Error

func Error(t assert.TestingT, err *GoGoError, msgAndArgs ...interface{}) bool

func GenerateJWT

func GenerateJWT(secret string, id int, email string) (string, error)

func InternalServerError

func InternalServerError(w http.ResponseWriter, err error)

func MethodNotAllowed

func MethodNotAllowed(w http.ResponseWriter, method string)

func NoError

func NoError(t assert.TestingT, err *GoGoError, msgAndArgs ...interface{}) bool

func NotFound

func NotFound(w http.ResponseWriter, message string)

Common error responses

func SendErrorResponse

func SendErrorResponse(w http.ResponseWriter, msg string, statusCode int)

func Unauthorized

func Unauthorized(w http.ResponseWriter)

func UniqueConstraintError

func UniqueConstraintError(err error) error

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data interface{})

WriteJSON writes a JSON response with the given status code and data

Types

type AuthAction

type AuthAction string
const (
	Register AuthAction = "create"
	Login    AuthAction = "login"
)

type AuthResponse

type AuthResponse struct {
	Email string `json:"email"`
	Token string `json:"token"`
}

type Claims

type Claims struct {
	ID    int    `json:"id"`
	Email string `json:"email"`
	jwt.RegisteredClaims
}

type ComparePassword

type ComparePassword func(hashedPassword, password []byte) error

type Credential

type Credential struct {
	Email    string     `json:"email"`
	Password string     `json:"password"`
	Action   AuthAction `json:"action"`
}

type Encryption

type Encryption interface {
	GenerateFromPassword(password []byte, cost int) ([]byte, error)
	CompareHashAndPassword(hashedPassword, password []byte) error
}

type ErrorResponse

type ErrorResponse struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
	Code    string `json:"code,omitempty"`
	Details string `json:"details,omitempty"`
}

type ErrorType

type ErrorType int
const (
	InvalidEmailFormat ErrorType = iota
	InvalidURIFormat
	InvalidPasswordLength
	InvalidUserId
	InvalidNameLength
	SQLError
	SQLUniqueViolated
	PasswordHashFailed
)

type GetJWT

type GetJWT func(secret string, id int, email string) (string, error)

type GoGoError

type GoGoError struct {
	Type    ErrorType
	Message string
	Err     error
}

func WrapError

func WrapError(err error, errorType ErrorType, message string) *GoGoError

func (*GoGoError) Error

func (e *GoGoError) Error() string

type HashPassword

type HashPassword func(password []byte, cost int) ([]byte, error)

type JWTHandler

type JWTHandler interface {
	GenerateJWT(secret string, id int, email string) (string, error)
	ParseWithClaims(tokenString string, claims jwt.Claims, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (*jwt.Token, error)
}

type ManagerRequest

type ManagerRequest struct {
	ID              int
	Email           *string `json:"email"`
	Password        *string `json:"password"`
	Name            *string `json:"name"`
	UserImageUri    *string `json:"userImageUri"`
	CompanyName     *string `json:"companyName"`
	CompanyImageUri *string `json:"companyImageUri"`
}

func (ManagerRequest) ValidCompanyImageURI

func (m ManagerRequest) ValidCompanyImageURI() bool

func (ManagerRequest) ValidCompanyName

func (m ManagerRequest) ValidCompanyName() bool

func (ManagerRequest) ValidEmail

func (m ManagerRequest) ValidEmail() bool

func (ManagerRequest) ValidImageURI

func (m ManagerRequest) ValidImageURI() bool

func (ManagerRequest) ValidName

func (m ManagerRequest) ValidName() bool

func (ManagerRequest) ValidPassword

func (m ManagerRequest) ValidPassword() bool

type ManagerResponse

type ManagerResponse struct {
	Email           string `json:"email"`
	Name            string `json:"name"`
	UserImageUri    string `json:"userImageUri"`
	CompanyName     string `json:"companyName"`
	CompanyImageUri string `json:"companyImageUri"`
}

type ParseJWT

type ParseJWT func(tokenString string, claims jwt.Claims, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (*jwt.Token, error)

type Response

type Response struct {
	Data    interface{} `json:"data,omitempty"`
	Message string      `json:"message,omitempty"`
}

For general response

Jump to

Keyboard shortcuts

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