Skip to content

a RESTful API for user management, including authentication and authorization

License

Notifications You must be signed in to change notification settings

josehenriquepg/microservice-user

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microservice User

This is a RESTful API for user management, including authentication and authorization. The API is built with Go, uses PostgreSQL as the database, and JSON Web Tokens (JWT) for secure authentication.

📚 Features

  • User registration
  • User login with JWT token generation
  • JWT-based authentication and authorization
  • User profile retrieval (protected route)

⚙️ How to use

Prerequisites

  • Go installed on your machine
  • PostgreSQL installed and running

Steps

  1. Clone the repository:
git clone https://github.com/yourusername/microservice-user.git
cd user-management-api
  1. Install dependencies:
go mod tidy
  1. Create a .env file in the root directory with the following content:
JWT_SECRET_KEY=your_generated_secret_key
DB_HOST=localhost
DB_USER=youruser
DB_NAME=yourdb
DB_PASSWORD=yourpassword
DB_PORT=5432
  1. Run PostgreSQL and create the database:
CREATE DATABASE yourdb;
  1. Update the config/config.go file with your PostgreSQL credentials if needed:
dsn := "host=localhost user=youruser dbname=yourdb sslmode=disable password=yourpassword"
  1. Run the application:
go run main.go

The API will be available at http://localhost:8080.

API Endpoints

Register User

  • URL: /register

  • Method: POST

  • Request Body:

      {
        "username": "yourusername",
        "password": "yourpassword"
      }
  • Response:

    • 201 Created

        {
          "message": "User registered successfully"
        }

Login User

  • URL: /login

  • Method: POST

  • Request Body:

      {
        "username": "yourusername",
        "password": "yourpassword"
      }
  • Response:

    • 200 OK

        {
          "token": "your.jwt.token"
        }
    • 401 Unauthorized

        {
          "error": "Invalid username or password"
        }

Get User Profile (Protected Route)

  • URL: /auth/profile
  • Method: GET
  • Headers:
    • Authorization: Bearer your.jwt.token
  • Response:
    • 200 OK
        {
          "message": "Authenticated",
          "username": "yourusername"
        }
    • 401 Unauthorized
        {
          "error": "Unauthorized"
        }

💼 License

This project is licensed under the MIT License.

💻 Technologies used

techs

  • Gin (web framework)
  • GORM (ORM)
  • JWT (JSON Web Tokens)
  • godotenv (environment variables)

🤝 Collaborators

Foto de José Henrique no GitHub
José Henrique

About

a RESTful API for user management, including authentication and authorization

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages