user

package
v0.0.0-...-5bf2025 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserExists         = errors.New("user already exists")
	ErrInvalidCredentials = errors.New("invalid eamil or password")
)

Functions

This section is empty.

Types

type Handler

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

func NewHandler

func NewHandler(service *Service) *Handler

func (*Handler) GetMe

func (h *Handler) GetMe(c *gin.Context)

swagger:operation GET /me users getMe --- tags: [users] description: Get current authenticated user details security: - bearerAuth: [] responses:

200:
  description: Successfully retrieved user details
  schema:
    type: object
    properties:
      id:
        type: string
        description: User ID in hex format
        example: 5f7d8f9e0c1d2e3f4a5b6c7d
      email:
        type: string
        format: email
        example: user@example.com
      role:
        type: string
        enum: [user, admin]
        example: user
401:
  description: Unauthorized - missing or invalid token
  schema:
    type: object
    properties:
      error:
        type: string
        example: unauthorized

func (*Handler) Login

func (h *Handler) Login(c *gin.Context)

swagger:operation POST /login users login --- tags: [users] description: Authenticate a user parameters:

  • name: body in: body required: true schema: {$ref: "#/definitions/LoginRequest"}

responses:

200:
  description: Successfully authenticated
  schema:
    type: object
    properties:
      token:
        type: string
        description: JWT access token
        example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
      user:
        type: object
        description: Authenticated user details
        properties:
          id:
            type: string
            description: User ID in hex format
            example: 5f7d8f9e0c1d2e3f4a5b6c7d
          email:
            type: string
            format: email
            example: user@example.com
          role:
            type: string
            enum: [user, admin]
            example: user
400:
  description: Invalid request format
  schema:
    type: object
    properties:
      error:
        type: string
        example: invalid email format
401:
  description: Authentication failed
  schema:
    type: object
    properties:
      error:
        type: string
        example: invalid credentials
500:
  description: Internal server error
  schema:
    type: object
    properties:
      error:
        type: string
        example: failed to generate auth token

func (*Handler) RegisterAdmin

func (h *Handler) RegisterAdmin(c *gin.Context)

swagger:operation POST /admin/register admin registerAdmin --- tags: [admin] description: Register a new admin account (requires admin privileges) security: - bearerAuth: [] parameters:

  • name: body in: body required: true schema: {$ref: "#/definitions/RegisterRequest"}

responses:

201:
  description: Admin user created successfully
400:
  description: Bad request
  schema:
    type: object
    properties:
      error:
        type: string
        example: "Invalid password format"
409:
  description: Conflict
  schema:
    type: object
    properties:
      error:
        type: string
        example: "user already exists"
500:
  description: Internal server error
  schema:
    type: object
    properties:
      error:
        type: string
        example: "failed to create admin user"

func (*Handler) RegisterUser

func (h *Handler) RegisterUser(c *gin.Context)

swagger:operation POST /register users registerUser --- tags: [users] description: Register a new user account

parameters:

  • name: body in: body description: User registration information required: true schema: {$ref: "#/definitions/RegisterRequest"}

responses:

201:
  description: User created successfully
400:
  description: Bad request
  schema:
    type: object
    properties:
      error:
        type: string
        example: "Invalid email format"
409:
  description: Conflict
  schema:
    type: object
    properties:
      error:
        type: string
        example: "user already exists"
500:
  description: Internal server error
  schema:
    type: object
    properties:
      error:
        type: string
        example: "failed to create user"

type LoginRequest

type LoginRequest struct {
	// Email of the user
	// required: true
	// example: user@example.com
	Email string `json:"email" binding:"required,email"`

	// Password of the user
	// required: true
	// example: password123
	Password string `json:"password" binding:"required,min=8"`
}

swagger:model LoginRequest

type RegisterRequest

type RegisterRequest struct {
	// Email of the user
	// required: true
	// example: user@example.com
	Email string `json:"email" binding:"required,email"`

	// Password must be at least 8 characters long
	// required: true
	// example: password123
	Password string `json:"password" binding:"required,min=8"`
}

swagger:model RegisterRequest

type Repository

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

func NewRepository

func NewRepository(db *mongo.Database) *Repository

func (*Repository) CreateUser

func (r *Repository) CreateUser(ctx context.Context, user *User) error

func (*Repository) GetUserByEmail

func (r *Repository) GetUserByEmail(ctx context.Context, email string) (*User, error)

type Service

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

func NewService

func NewService(repo *Repository) *Service

func (*Service) Login

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

func (*Service) Register

func (s *Service) Register(ctx context.Context, email, password string, isAdmin bool) error

type User

type User struct {
	ID       bson.ObjectID `bson:"_id,omitempty" json:"id"`
	Email    string        `bson:"email" json:"email"`
	Password string        `bson:"password,omitempty" json:"-"`
	Role     string        `bson:"role" json:"-"`
}

Jump to

Keyboard shortcuts

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