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.
- Initialization: The application loads environment variables using
godotenvand initializes the JWT secret key. - Routing: The application uses Gorilla Mux to handle HTTP requests and routes them to the appropriate handlers.
- Authentication: JWT is used for authentication, with middleware to protect certain endpoints.
- Token Management: Tokens can be generated, validated, refreshed, and revoked.
- User Storage: User credentials are stored in Redis.
- URL:
/signup - Method:
POST - Description: Registers a new user.
- Request Body: JSON containing
emailandpassword. - Curl Command:
curl -X POST http://localhost:8000/signup -H "Content-Type: application/json" -d '{"email":"user@example.com", "password":"password123"}'
- URL:
/signin - Method:
POST - Description: Authenticates a user and returns a JWT.
- Request Body: JSON containing
emailandpassword. - Curl Command:
curl -X POST http://localhost:8000/signin -H "Content-Type: application/json" -d '{"email":"user@example.com", "password":"password123"}'
- 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>"
- 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>"
- 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>"
- JWT_SECRET: Secret key used for signing JWTs. This should be set in a
.envfile. - REDIS_URL: URL for the Redis service, typically
redis:6379when using Docker Compose.
The Dockerfile is used to build the application image. It should be located in the root directory of your project.
The docker-compose.yml file is used to define and run multi-container Docker applications, including the Redis service.
-
Build and Run with Docker Compose:
docker-compose up --build
-
Access the Application: The application will be available at
http://localhost:8000.