user

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeUserNotFound          = "USER_NOT_FOUND"
	CodeUserAlreadyExists     = "USER_ALREADY_EXISTS"
	CodeEmailAlreadyExists    = "EMAIL_ALREADY_EXISTS"
	CodeUsernameAlreadyExists = "USERNAME_ALREADY_EXISTS"
	CodeInvalidEmail          = "INVALID_EMAIL"
	CodeInvalidUsername       = "INVALID_USERNAME"
	CodeWeakPassword          = "WEAK_PASSWORD"
	CodeUserCreationFailed    = "USER_CREATION_FAILED"
	CodeUserUpdateFailed      = "USER_UPDATE_FAILED"
	CodeUserDeletionFailed    = "USER_DELETION_FAILED"
	CodeInvalidUserData       = "INVALID_USER_DATA"
	CodeEmailTaken            = "EMAIL_TAKEN"
	CodeUsernameTaken         = "USERNAME_TAKEN"
)

Variables

View Source
var (
	ErrUserNotFound          = &errs.AuthsomeError{Code: CodeUserNotFound}
	ErrUserAlreadyExists     = &errs.AuthsomeError{Code: CodeUserAlreadyExists}
	ErrEmailAlreadyExists    = &errs.AuthsomeError{Code: CodeEmailAlreadyExists}
	ErrUsernameAlreadyExists = &errs.AuthsomeError{Code: CodeUsernameAlreadyExists}
	ErrInvalidEmail          = &errs.AuthsomeError{Code: CodeInvalidEmail}
	ErrInvalidUsername       = &errs.AuthsomeError{Code: CodeInvalidUsername}
	ErrWeakPassword          = &errs.AuthsomeError{Code: CodeWeakPassword}
	ErrUserCreationFailed    = &errs.AuthsomeError{Code: CodeUserCreationFailed}
	ErrUserUpdateFailed      = &errs.AuthsomeError{Code: CodeUserUpdateFailed}
	ErrUserDeletionFailed    = &errs.AuthsomeError{Code: CodeUserDeletionFailed}
	ErrInvalidUserData       = &errs.AuthsomeError{Code: CodeInvalidUserData}
)

Functions

func EmailAlreadyExists

func EmailAlreadyExists(email string) *errs.AuthsomeError

EmailAlreadyExists returns an error when an email is already registered

func EmailTaken

func EmailTaken(email string) *errs.AuthsomeError

EmailTaken returns an error when email is taken in the same app

func InvalidEmail

func InvalidEmail(email string) *errs.AuthsomeError

InvalidEmail returns an error for invalid email format

func InvalidUserData

func InvalidUserData(field, reason string) *errs.AuthsomeError

InvalidUserData returns an error for invalid user data

func InvalidUsername

func InvalidUsername(username, reason string) *errs.AuthsomeError

InvalidUsername returns an error for invalid username format

func UserAlreadyExists

func UserAlreadyExists(email string) *errs.AuthsomeError

UserAlreadyExists returns an error when attempting to create a duplicate user

func UserCreationFailed

func UserCreationFailed(err error) *errs.AuthsomeError

UserCreationFailed returns an error when user creation fails

func UserDeletionFailed

func UserDeletionFailed(err error) *errs.AuthsomeError

UserDeletionFailed returns an error when user deletion fails

func UserNotFound

func UserNotFound(identifier string) *errs.AuthsomeError

UserNotFound returns an error when a user cannot be found

func UserUpdateFailed

func UserUpdateFailed(err error) *errs.AuthsomeError

UserUpdateFailed returns an error when user update fails

func UsernameAlreadyExists

func UsernameAlreadyExists(username string) *errs.AuthsomeError

UsernameAlreadyExists returns an error when a username is already taken

func UsernameTaken

func UsernameTaken(username string) *errs.AuthsomeError

UsernameTaken returns an error when username is taken

func WeakPassword

func WeakPassword(reason string) *errs.AuthsomeError

WeakPassword returns an error when password doesn't meet requirements

Types

type Ban

type Ban struct {
	ID           string
	UserID       xid.ID     `json:"userID"`
	BannedByID   xid.ID     `json:"bannedByID"`
	UnbannedByID *xid.ID    `json:"unbannedByID,omitempty"`
	Reason       string     `json:"reason"`
	IsActive     bool       `json:"isActive"`
	ExpiresAt    *time.Time `json:"expiresAt,omitempty"`
	CreatedAt    time.Time  `json:"createdAt"`
	UpdatedAt    time.Time  `json:"updatedAt"`
	UnbannedAt   *time.Time `json:"unbannedAt,omitempty"`
}

Ban represents a user ban with business logic

type BanRepository

type BanRepository interface {
	// Create a new ban record
	CreateBan(ctx context.Context, ban *schema.UserBan) error

	// Find active ban for a user
	FindActiveBan(ctx context.Context, userID string) (*schema.UserBan, error)

	// Find all bans for a user (including inactive)
	FindBansByUser(ctx context.Context, userID string) ([]*schema.UserBan, error)

	// Update ban record (for unbanning)
	UpdateBan(ctx context.Context, ban *schema.UserBan) error

	// Find ban by ID
	FindBanByID(ctx context.Context, banID string) (*schema.UserBan, error)
}

BanRepository defines the interface for user ban operations

type BanRequest

type BanRequest struct {
	UserID    xid.ID `json:"userID"`
	Reason    string `json:"reason"`
	BannedBy  xid.ID `json:"bannedBy"`
	ExpiresAt *time.Time
}

BanRequest represents a request to ban a user

type BanService

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

BanService handles user banning operations

func NewBanService

func NewBanService(banRepo BanRepository) *BanService

NewBanService creates a new ban service

func (*BanService) BanUser

func (s *BanService) BanUser(ctx context.Context, req *BanRequest) (*Ban, error)

BanUser bans a user with the given reason and optional expiration

func (*BanService) CheckBan

func (s *BanService) CheckBan(ctx context.Context, userID string) (*Ban, error)

CheckBan checks if a user is currently banned

func (*BanService) GetUserBans

func (s *BanService) GetUserBans(ctx context.Context, userID string) ([]*Ban, error)

GetUserBans returns all bans for a user (including inactive)

func (*BanService) IsUserBanned

func (s *BanService) IsUserBanned(ctx context.Context, userID string) (bool, error)

IsUserBanned checks if a user is currently banned (convenience method)

func (*BanService) SetHookRegistry added in v0.0.6

func (s *BanService) SetHookRegistry(registry interface{})

SetHookRegistry sets the hook registry for executing lifecycle hooks

func (*BanService) UnbanUser

func (s *BanService) UnbanUser(ctx context.Context, req *UnbanRequest) error

UnbanUser removes an active ban from a user

type Config

type Config struct {
	PasswordRequirements validator.PasswordRequirements

	ChangeEmail struct {
		Enabled             bool
		RequireVerification bool
	}

	DeleteAccount struct {
		RequirePassword     bool
		RequireVerification bool
	}
}

Config represents user service configuration

type CountUsersFilter

type CountUsersFilter struct {
	AppID        xid.ID     `json:"appId"`
	CreatedSince *time.Time `json:"createdSince,omitempty"`
}

CountUsersFilter represents filter parameters for counting users

type CreateUserRequest

type CreateUserRequest struct {
	AppID    xid.ID `json:"appId" validate:"required"`
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required"`
	Name     string `json:"name"`
}

CreateUserRequest represents a create user request

type HookExecutor added in v0.0.3

type HookExecutor interface {
	ExecuteBeforeUserCreate(ctx context.Context, req *CreateUserRequest) error
	ExecuteAfterUserCreate(ctx context.Context, user *User) error
	ExecuteBeforeUserUpdate(ctx context.Context, userID xid.ID, req *UpdateUserRequest) error
	ExecuteAfterUserUpdate(ctx context.Context, user *User) error
	ExecuteBeforeUserDelete(ctx context.Context, userID xid.ID) error
	ExecuteAfterUserDelete(ctx context.Context, userID xid.ID) error
}

HookExecutor defines the interface for executing user-related hooks This interface allows the user service to execute hooks without importing the hooks package, avoiding circular dependencies (hooks package imports user for types)

type ListUsersFilter

type ListUsersFilter struct {
	pagination.PaginationParams
	AppID         xid.ID  `json:"appId" query:"app_id"`
	EmailVerified *bool   `json:"emailVerified,omitempty" query:"email_verified"`
	Search        *string `json:"search,omitempty" query:"search"`
}

ListUsersFilter represents filter parameters for listing users

type ListUsersResponse

type ListUsersResponse = pagination.PageResponse[*User]

type Repository

type Repository interface {
	// Create creates a new user
	Create(ctx context.Context, user *schema.User) error

	// FindByID finds a user by ID
	FindByID(ctx context.Context, id xid.ID) (*schema.User, error)

	// FindByEmail finds a user by email (global search)
	FindByEmail(ctx context.Context, email string) (*schema.User, error)

	// FindByAppAndEmail finds a user by app ID and email (app-scoped search)
	FindByAppAndEmail(ctx context.Context, appID xid.ID, email string) (*schema.User, error)

	// FindByUsername finds a user by username
	FindByUsername(ctx context.Context, username string) (*schema.User, error)

	// Update updates a user
	Update(ctx context.Context, user *schema.User) error

	// Delete deletes a user by ID
	Delete(ctx context.Context, id xid.ID) error

	// ListUsers lists users with pagination and filtering
	ListUsers(ctx context.Context, filter *ListUsersFilter) (*pagination.PageResponse[*schema.User], error)

	// CountUsers counts users with filtering
	CountUsers(ctx context.Context, filter *CountUsersFilter) (int, error)
}

Repository defines the interface for user storage operations This follows the Interface Segregation Principle from core/app architecture

type Service

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

Service provides user-related operations

func NewService

func NewService(repo Repository, cfg Config, webhookSvc *webhook.Service, hookExecutor HookExecutor) *Service

NewService creates a new user service

func (*Service) CountUsers

func (s *Service) CountUsers(ctx context.Context, filter *CountUsersFilter) (int, error)

CountUsers counts users with filtering

func (*Service) Create

func (s *Service) Create(ctx context.Context, req *CreateUserRequest) (*User, error)

Create creates a new user in the specified app

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id xid.ID) error

Delete deletes a user by ID

func (*Service) FindByAppAndEmail

func (s *Service) FindByAppAndEmail(ctx context.Context, appID xid.ID, email string) (*User, error)

FindByAppAndEmail finds a user by app ID and email (app-scoped search)

func (*Service) FindByEmail

func (s *Service) FindByEmail(ctx context.Context, email string) (*User, error)

FindByEmail finds a user by email (global search, not app-scoped)

func (*Service) FindByID

func (s *Service) FindByID(ctx context.Context, id xid.ID) (*User, error)

FindByID finds a user by ID

func (*Service) FindByUsername

func (s *Service) FindByUsername(ctx context.Context, username string) (*User, error)

FindByUsername finds a user by username

func (*Service) GetHookRegistry added in v0.0.6

func (s *Service) GetHookRegistry() interface{}

GetHookRegistry returns the hook registry

func (*Service) GetVerificationRepo added in v0.0.6

func (s *Service) GetVerificationRepo() interface{}

GetVerificationRepo returns the verification repository

func (*Service) ListUsers

func (s *Service) ListUsers(ctx context.Context, filter *ListUsersFilter) (*pagination.PageResponse[*User], error)

ListUsers lists users with pagination and filtering

func (*Service) SetHookRegistry added in v0.0.6

func (s *Service) SetHookRegistry(registry interface{})

SetHookRegistry sets the hook registry for executing lifecycle hooks

func (*Service) SetVerificationRepo added in v0.0.6

func (s *Service) SetVerificationRepo(repo interface{})

SetVerificationRepo sets the verification repository for password resets

func (*Service) Update

func (s *Service) Update(ctx context.Context, u *User, req *UpdateUserRequest) (*User, error)

Update updates a user

func (*Service) UpdatePassword added in v0.0.6

func (s *Service) UpdatePassword(ctx context.Context, userID xid.ID, hashedPassword string) error

UpdatePassword updates a user's password directly

type ServiceInterface

type ServiceInterface interface {
	// Create creates a new user in the specified app
	Create(ctx context.Context, req *CreateUserRequest) (*User, error)

	// FindByID finds a user by ID
	FindByID(ctx context.Context, id xid.ID) (*User, error)

	// FindByEmail finds a user by email (global search)
	FindByEmail(ctx context.Context, email string) (*User, error)

	// FindByAppAndEmail finds a user by app ID and email (app-scoped search)
	FindByAppAndEmail(ctx context.Context, appID xid.ID, email string) (*User, error)

	// FindByUsername finds a user by username
	FindByUsername(ctx context.Context, username string) (*User, error)

	// Update updates a user
	Update(ctx context.Context, u *User, req *UpdateUserRequest) (*User, error)

	// UpdatePassword updates a user's password directly
	UpdatePassword(ctx context.Context, userID xid.ID, hashedPassword string) error

	// Delete deletes a user by ID
	Delete(ctx context.Context, id xid.ID) error

	// ListUsers lists users with pagination and filtering
	ListUsers(ctx context.Context, filter *ListUsersFilter) (*pagination.PageResponse[*User], error)

	// CountUsers counts users with filtering
	CountUsers(ctx context.Context, filter *CountUsersFilter) (int, error)

	// SetHookRegistry sets the hook registry for lifecycle events
	SetHookRegistry(registry interface{})

	// GetHookRegistry returns the hook registry
	GetHookRegistry() interface{}

	// SetVerificationRepo sets the verification repository for password resets
	SetVerificationRepo(repo interface{})

	// GetVerificationRepo returns the verification repository
	GetVerificationRepo() interface{}
}

ServiceInterface defines the contract for user service operations This allows plugins to decorate the service with additional behavior

type UnbanRequest

type UnbanRequest struct {
	UserID     xid.ID `json:"userID"`
	UnbannedBy xid.ID `json:"unbannedBy"`
	Reason     string
}

UnbanRequest represents a request to unban a user

type UpdateUserRequest

type UpdateUserRequest struct {
	Name            *string `json:"name,omitempty"`
	Email           *string `json:"email,omitempty" validate:"omitempty,email"`
	EmailVerified   *bool   `json:"emailVerified,omitempty"`
	Image           *string `json:"image,omitempty"`
	Username        *string `json:"username,omitempty"`
	DisplayUsername *string `json:"displayUsername,omitempty"`
}

UpdateUserRequest represents an update user request

type User

type User = base.User

User represents a user entity DTO This is separate from schema.User to maintain proper separation of concerns

func FromSchemaUser

func FromSchemaUser(su *schema.User) *User

FromSchemaUser converts a schema.User model to User DTO

func FromSchemaUsers

func FromSchemaUsers(users []*schema.User) []*User

FromSchemaUsers converts a slice of schema.User to User DTOs

Jump to

Keyboard shortcuts

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