user

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 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 = domainerrors.New(domainerrors.ErrCodeNotFound, "user not found", nil)
	// ErrEmailAlreadyExists indicates that a user with the given email already exists
	ErrEmailAlreadyExists = domainerrors.New(domainerrors.ErrCodeAlreadyExists, "email already exists", nil)
	// ErrInvalidCredentials indicates that the provided credentials are invalid
	ErrInvalidCredentials = domainerrors.New(domainerrors.ErrCodeAuthentication, "invalid credentials", nil)
	// ErrUserExists indicates that a user with the given email already exists
	ErrUserExists = domainerrors.New(domainerrors.ErrCodeAlreadyExists, "user already exists", nil)
	// ErrInvalidToken indicates that the provided token is invalid
	ErrInvalidToken = domainerrors.New(domainerrors.ErrCodeInvalidToken, "invalid token", nil)
	// ErrTokenExpired indicates that the provided token has expired
	ErrTokenExpired = domainerrors.New(domainerrors.ErrCodeInvalidToken, "token has expired", nil)
)
View Source
var ErrInvalidUserID = errors.New("invalid user ID")

ErrInvalidUserID indicates that the provided user ID is invalid

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 LoginResponse added in v0.1.5

type LoginResponse struct {
	User  *User      `json:"user"`
	Token *TokenPair `json:"token"`
}

LoginResponse represents the response from a successful login

type Repository added in v0.1.5

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

Repository defines the interface for user storage (renamed from Store for consistency)

type Service

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

Service defines the user service interface

func NewService

func NewService(repo Repository, logger logging.Logger) Service

NewService creates a new user service

type ServiceImpl

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

ServiceImpl implements the Service interface

func (*ServiceImpl) Authenticate added in v0.1.5

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

Authenticate matches the domain.UserService interface

func (*ServiceImpl) DeleteUser

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

DeleteUser deletes a user

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 string) (*User, error)

GetUserByID retrieves a user by ID

func (*ServiceImpl) GetUserIDFromToken

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

GetUserIDFromToken extracts the user ID from a token

func (*ServiceImpl) IsTokenBlacklisted

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

IsTokenBlacklisted checks if a token is blacklisted

func (*ServiceImpl) ListUsers

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

ListUsers lists all users

func (*ServiceImpl) Login

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

Login authenticates a user

func (*ServiceImpl) Logout

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

Logout blacklists 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 a user

func (*ServiceImpl) ValidateToken

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

ValidateToken validates a token

type Signup

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

Signup represents the user signup request

type TokenPair

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

TokenPair represents a pair of access and refresh tokens

type User

type User struct {
	ID             string         `json:"id" gorm:"column:uuid;primaryKey;type:uuid;default:gen_random_uuid()"`
	Email          string         `json:"email" gorm:"uniqueIndex;not null;size:255"`
	HashedPassword string         `json:"-" gorm:"column:hashed_password;not null;size:255"`
	FirstName      string         `json:"first_name" gorm:"not null;size:100"`
	LastName       string         `json:"last_name" gorm:"not null;size:100"`
	Role           string         `json:"role" gorm:"not null;size:50;default:user"`
	Active         bool           `json:"active" gorm:"not null;default:true"`
	CreatedAt      time.Time      `json:"created_at" gorm:"not null;autoCreateTime"`
	UpdatedAt      time.Time      `json:"updated_at" gorm:"not null;autoUpdateTime"`
	DeletedAt      gorm.DeletedAt `json:"-" gorm:"index"`
}

User represents a user in the system

func (*User) BeforeCreate added in v0.2.0

func (u *User) BeforeCreate(tx *gorm.DB) error

BeforeCreate is a GORM hook that runs before creating a user

func (*User) BeforeUpdate added in v0.2.0

func (u *User) BeforeUpdate(tx *gorm.DB) error

BeforeUpdate is a GORM hook that runs before updating a user

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

func (*User) TableName added in v0.2.0

func (u *User) TableName() string

TableName specifies the table name for the User model

Jump to

Keyboard shortcuts

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