Skip to content

ReCodee/go-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Auth Service

This project is a simple authentication service built with Go, using JWT for authentication and Docker for containerization. It now uses Redis for storing user credentials.

Project Flow

  1. Initialization: The application loads environment variables using godotenv and initializes the JWT secret key.
  2. Routing: The application uses Gorilla Mux to handle HTTP requests and routes them to the appropriate handlers.
  3. Authentication: JWT is used for authentication, with middleware to protect certain endpoints.
  4. Token Management: Tokens can be generated, validated, refreshed, and revoked.
  5. User Storage: User credentials are stored in Redis.

Endpoints

1. Sign Up

  • URL: /signup
  • Method: POST
  • Description: Registers a new user.
  • Request Body: JSON containing email and password.
  • Curl Command:
    curl -X POST http://localhost:8000/signup -H "Content-Type: application/json" -d '{"email":"user@example.com", "password":"password123"}'

2. Sign In

  • URL: /signin
  • Method: POST
  • Description: Authenticates a user and returns a JWT.
  • Request Body: JSON containing email and password.
  • Curl Command:
    curl -X POST http://localhost:8000/signin -H "Content-Type: application/json" -d '{"email":"user@example.com", "password":"password123"}'

3. Refresh Token

  • URL: /refresh
  • Method: POST
  • Description: Refreshes an existing JWT if it's close to expiration.
  • Headers: Authorization: Bearer <token>
  • Curl Command:
    curl -X POST http://localhost:8000/refresh -H "Authorization: Bearer <token>"

4. Revoke Token

  • URL: /revoke
  • Method: POST
  • Description: Revokes a JWT.
  • Headers: Authorization: Bearer <token>
  • Curl Command:
    curl -X POST http://localhost:8000/revoke -H "Authorization: Bearer <token>"

5. Protected Endpoint

  • URL: /protected
  • Method: GET
  • Description: Accesses a protected resource.
  • Headers: Authorization: Bearer <token>
  • Curl Command:
    curl -X GET http://localhost:8000/protected -H "Authorization: Bearer <token>"

Environment Variables

  • JWT_SECRET: Secret key used for signing JWTs. This should be set in a .env file.
  • REDIS_URL: URL for the Redis service, typically redis:6379 when using Docker Compose.

Dockerfile

The Dockerfile is used to build the application image. It should be located in the root directory of your project.

Docker Compose

The docker-compose.yml file is used to define and run multi-container Docker applications, including the Redis service.

Running the Application

  1. Build and Run with Docker Compose:

    docker-compose up --build
  2. Access the Application: The application will be available at http://localhost:8000.

About

Simple auth application made in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages