user

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAccountLocked = errors.New("account is temporarily locked due to too many failed login attempts")

ErrAccountLocked is returned when the account is temporarily locked due to too many failed login attempts.

Functions

func CountUsers added in v1.0.0

func CountUsers() (int, error)

CountUsers returns the total number of users in the database.

func DeleteUser

func DeleteUser(id string) error

func HandleCreateUser

func HandleCreateUser(w http.ResponseWriter, r *http.Request)

HandleCreateUser godoc @Summary Create a new user @Description Registers a new user in the system @Tags users @Accept json @Produce json @Param user body UserCreateRequest true "User creation payload" @Success 201 {object} UserResponse @Failure 400 {object} model.ApiError @Failure 500 {object} model.ApiError @Router /users/create [post]

func HandleDeleteUser added in v1.0.0

func HandleDeleteUser(w http.ResponseWriter, r *http.Request)

HandleDeleteUser handles DELETE /user/{id}

func HandleGetUser added in v1.0.0

func HandleGetUser(w http.ResponseWriter, r *http.Request)

HandleGetUser handles GET /user/{id} (read user by ID)

func HandleListUsers added in v1.0.0

func HandleListUsers(w http.ResponseWriter, r *http.Request)

HandleListUsers handles GET /admin/api/users - lists all active users

func HandleUnlockUser added in v1.0.0

func HandleUnlockUser(w http.ResponseWriter, r *http.Request)

HandleUnlockUser unlocks a user account after multiple failed login attempts. @Summary Unlock user account @Description Resets the failed login attempts and clear the lockout time for a user. @Tags users-admin @Accept json @Produce json @Param id query string true "User ID" @Security BearerAuth @Success 200 {object} UserResponse @Router /admin/api/users/unlock [post]

func HandleUpdateUser added in v1.0.0

func HandleUpdateUser(w http.ResponseWriter, r *http.Request)

HandleUpdateUser handles PUT /user/{id} (update user)

func HandleUserAdminEndpoint added in v1.0.0

func HandleUserAdminEndpoint(w http.ResponseWriter, r *http.Request)

HandleUserAdminEndpoint is the combined handler for /admin/api/users Routes requests based on HTTP method @Summary User administration @Description GET: List users or get user by ID. POST: Create user. PUT: Update user. DELETE: Soft-delete user. @Tags users-admin @Accept json @Produce json @Param id query string false "User ID (required for GET/PUT/DELETE single)" @Param user body UserCreateRequest false "User creation/update payload" @Security BearerAuth @Success 200 {object} UserResponse "Single user (GET/PUT)" @Success 200 {array} UserResponse "List of users (GET)" @Success 201 {object} UserResponse "Created user (POST)" @Router /admin/api/users [get] @Router /admin/api/users [post] @Router /admin/api/users [put] @Router /admin/api/users [delete]

func SaveTotpSecret added in v1.0.0

func SaveTotpSecret(userID, secret string) error

func UnlockUser added in v1.0.0

func UnlockUser(id string) error

func UpdateUser

func UpdateUser(id string, req UserUpdateRequest) error

func ValidateUserCreateRequest

func ValidateUserCreateRequest(input UserCreateRequest) error

func ValidateUserUpdateRequest added in v1.0.0

func ValidateUserUpdateRequest(input UserUpdateRequest) error

Types

type ApiUserResponse

type ApiUserResponse struct {
	Data  *UserResponse   `json:"data,omitempty"`
	Error *model.ApiError `json:"error,omitempty"`
}

ApiUserResponse is used for Swagger documentation

type User

type User struct {
	ID                  string
	Username            string
	Password            string
	Email               string
	CreatedAt           time.Time
	Role                string
	FailedLoginAttempts int
	LockedUntil         *time.Time
	TotpSecret          string
	TotpVerified        bool
	IsEmailVerified     bool
	DeactivatedAt       *time.Time
}

func AuthenticateUser

func AuthenticateUser(username, password string) (*User, error)

AuthenticateUser checks if the provided username and password match a user in the database. It enforces account lockout after repeated failed attempts when configured.

func ListUsers added in v1.0.0

func ListUsers() ([]*User, error)

func UserByID

func UserByID(userID string) (*User, error)

func UserByUsername added in v1.0.0

func UserByUsername(username string) (*User, error)

func (*User) ToResponse added in v1.0.0

func (u *User) ToResponse() UserResponse

type UserCreateRequest

type UserCreateRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email,omitempty"`
	Role     string `json:"role,omitempty"` // optional role assignment
}

type UserResponse

type UserResponse struct {
	ID                  string     `json:"id"`
	Username            string     `json:"username"`
	Email               string     `json:"email"`
	CreatedAt           time.Time  `json:"created_at"`
	Role                string     `json:"role"`
	FailedLoginAttempts int        `json:"failed_login_attempts"`
	LockedUntil         *time.Time `json:"locked_until,omitempty"`
	IsEmailVerified     bool       `json:"is_email_verified"`
	TotpVerified        bool       `json:"totp_verified"`
}

func CreateUser

func CreateUser(username, password, email string) (*UserResponse, error)

type UserUpdateRequest added in v1.0.0

type UserUpdateRequest struct {
	Username        string `json:"username,omitempty"`
	Password        string `json:"password,omitempty"`
	Email           string `json:"email,omitempty"`
	Role            string `json:"role,omitempty"`
	IsEmailVerified *bool  `json:"is_email_verified,omitempty"`
	TotpVerified    *bool  `json:"totp_verified,omitempty"`
}

Jump to

Keyboard shortcuts

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