user

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 23, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUserNotFound indicates that a user was not found
	ErrUserNotFound = errors.New("user not found")
	// ErrEmailAlreadyExists indicates that a user with the given email already exists
	ErrEmailAlreadyExists = errors.New("email already exists")
	// ErrInvalidCredentials indicates that the provided credentials are invalid
	ErrInvalidCredentials = errors.New("invalid credentials")
	// ErrInvalidToken indicates that the provided token is invalid
	ErrInvalidToken = errors.New("invalid token")
	// ErrTokenBlacklisted indicates that the token has been blacklisted
	ErrTokenBlacklisted = errors.New("token is blacklisted")
	// ErrInvalidUserIDClaim indicates that the user_id claim type is invalid
	ErrInvalidUserIDClaim = errors.New("invalid user_id claim type")
	// ErrInvalidUserID indicates that the user_id claim type is invalid
	ErrInvalidUserID = errors.New("invalid user_id claim type")
	// ErrUserExists indicates that a user with the given email already exists
	ErrUserExists = errors.New("user already exists")
)

Functions

This section is empty.

Types

type Login

type Login struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required"`
}

Login represents the user login request

type Service

type Service interface {
	SignUp(ctx context.Context, signup *Signup) (*User, error)
	Login(ctx context.Context, login *Login) (*TokenPair, error)
	Logout(ctx context.Context, token string) error
	RefreshToken(ctx context.Context, refreshToken string) (*TokenPair, error)
	GetUserByID(ctx context.Context, id uint) (*User, error)
	GetUserByEmail(ctx context.Context, email string) (*User, error)
	UpdateUser(ctx context.Context, user *User) error
	DeleteUser(ctx context.Context, id uint) error
	ListUsers(ctx context.Context) ([]User, error)
	ValidateToken(token string) (*jwt.Token, error)
	IsTokenBlacklisted(token string) bool
	GetUserIDFromToken(token string) (string, error)
	GetByID(ctx context.Context, id string) (*User, error)
}

Service defines the user service interface

func NewService

func NewService(store Store, logger logging.Logger, jwtSecret string) Service

NewService creates a new user service

type ServiceImpl

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

ServiceImpl implements the Service interface

func (*ServiceImpl) DeleteUser

func (s *ServiceImpl) DeleteUser(ctx context.Context, id uint) error

DeleteUser removes a user by ID

func (*ServiceImpl) GetByID

func (s *ServiceImpl) GetByID(ctx context.Context, id string) (*User, error)

GetByID retrieves a user by ID string

func (*ServiceImpl) GetUserByEmail

func (s *ServiceImpl) GetUserByEmail(ctx context.Context, email string) (*User, error)

GetUserByEmail retrieves a user by email

func (*ServiceImpl) GetUserByID

func (s *ServiceImpl) GetUserByID(ctx context.Context, id uint) (*User, error)

GetUserByID retrieves a user by ID

func (*ServiceImpl) GetUserIDFromToken

func (s *ServiceImpl) GetUserIDFromToken(token string) (string, error)

GetUserIDFromToken retrieves the user ID from a token

func (*ServiceImpl) IsTokenBlacklisted

func (s *ServiceImpl) IsTokenBlacklisted(token string) bool

IsTokenBlacklisted checks if a token is in the blacklist

func (*ServiceImpl) ListUsers

func (s *ServiceImpl) ListUsers(ctx context.Context) ([]User, error)

ListUsers returns all users

func (*ServiceImpl) Login

func (s *ServiceImpl) Login(ctx context.Context, login *Login) (*TokenPair, error)

Login authenticates a user and returns a token pair

func (*ServiceImpl) Logout

func (s *ServiceImpl) Logout(ctx context.Context, token string) error

Logout adds a token to the blacklist

func (*ServiceImpl) RefreshToken

func (s *ServiceImpl) RefreshToken(ctx context.Context, refreshToken string) (*TokenPair, error)

RefreshToken generates a new token pair using a refresh token

func (*ServiceImpl) SignUp

func (s *ServiceImpl) SignUp(ctx context.Context, signup *Signup) (*User, error)

SignUp registers a new user

func (*ServiceImpl) UpdateUser

func (s *ServiceImpl) UpdateUser(ctx context.Context, user *User) error

UpdateUser updates an existing user

func (*ServiceImpl) ValidateToken

func (s *ServiceImpl) ValidateToken(tokenString string) (*jwt.Token, error)

ValidateToken validates a JWT token

type Signup

type Signup struct {
	Email     string `json:"email" validate:"required,email"`
	Password  string `json:"password" validate:"required,min=8"`
	FirstName string `json:"first_name" validate:"required"`
	LastName  string `json:"last_name" validate:"required"`
}

Signup represents the user signup request

type Store

type Store interface {
	Create(ctx context.Context, user *User) error
	GetByID(ctx context.Context, id uint) (*User, error)
	GetByEmail(ctx context.Context, email string) (*User, error)
	Update(ctx context.Context, user *User) error
	Delete(ctx context.Context, id uint) error
	List(ctx context.Context) ([]User, error)
}

Store defines the interface for user data operations

type TokenPair

type TokenPair struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

TokenPair represents an access and refresh token pair

type User

type User struct {
	ID             uint      `json:"id" db:"id"`
	Email          string    `json:"email" db:"email"`
	HashedPassword string    `json:"-" db:"hashed_password"`
	FirstName      string    `json:"first_name" db:"first_name"`
	LastName       string    `json:"last_name" db:"last_name"`
	Role           string    `json:"role" db:"role"`
	Active         bool      `json:"active" db:"active"`
	CreatedAt      time.Time `json:"created_at" db:"created_at"`
	UpdatedAt      time.Time `json:"updated_at" db:"updated_at"`
}

User represents a user in the system

func (*User) CheckPassword

func (u *User) CheckPassword(password string) bool

CheckPassword verifies if the provided password matches the user's hashed password

func (*User) SetPassword

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

SetPassword hashes and sets the user's password

Jump to

Keyboard shortcuts

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