{"id":29659322,"url":"https://github.com/noobconner21/go-jwt","last_synced_at":"2026-04-30T16:31:56.237Z","repository":{"id":304881584,"uuid":"1020111378","full_name":"noobconner21/go-jwt","owner":"noobconner21","description":"🔐 A secure REST API built with Go and Gin framework featuring JWT authentication, bcrypt password hashing, and PostgreSQL integration. Perfect starter template for Go web applications requiring user authentication.","archived":false,"fork":false,"pushed_at":"2025-07-15T20:44:38.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-16T18:46:11.703Z","etag":null,"topics":["authentication","gin","go","jwt","postgresql","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noobconner21.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-15T11:02:57.000Z","updated_at":"2025-07-15T20:47:55.000Z","dependencies_parsed_at":"2025-07-17T00:47:00.779Z","dependency_job_id":"c229e902-6b87-4c9f-a8d6-aaa2996ce8d0","html_url":"https://github.com/noobconner21/go-jwt","commit_stats":null,"previous_names":["noobconner21/go-jwt"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/noobconner21/go-jwt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noobconner21%2Fgo-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noobconner21%2Fgo-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noobconner21%2Fgo-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noobconner21%2Fgo-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noobconner21","download_url":"https://codeload.github.com/noobconner21/go-jwt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noobconner21%2Fgo-jwt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32470879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"ssl_error","status_checked_at":"2026-04-30T13:12:06.837Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","gin","go","jwt","postgresql","rest-api"],"created_at":"2025-07-22T09:10:36.703Z","updated_at":"2026-04-30T16:31:56.206Z","avatar_url":"https://github.com/noobconner21.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go JWT Authentication API\n\nA simple yet robust REST API built with Go (Gin framework) that provides JWT-based authentication with PostgreSQL database integration.\n\n## 🚀 Features\n\n- **User Registration** - Secure user signup with email and password\n- **User Authentication** - JWT-based login system\n- **Password Security** - Bcrypt hashing for password storage\n- **Token Validation** - Middleware for protecting routes\n- **Cookie-based JWT** - Secure token storage in HTTP-only cookies\n- **PostgreSQL Integration** - GORM ORM with PostgreSQL database\n- **Environment Configuration** - Secure configuration management\n\n## 🛠️ Tech Stack\n\n- **Language**: Go 1.24.5\n- **Framework**: Gin Web Framework\n- **Database**: PostgreSQL\n- **ORM**: GORM\n- **Authentication**: JWT (JSON Web Tokens)\n- **Password Hashing**: bcrypt\n- **Environment Management**: godotenv\n- **Development Tools**: CompileDaemon (hot reload)\n\n## 📁 Project Structure\n\n```\ngo-jwt/\n├── main.go                 # Application entry point\n├── go.mod                  # Go module file\n├── go.sum                  # Go module dependencies\n├── controllers/\n│   └── usersController.go  # User-related HTTP handlers\n├── initializers/\n│   ├── connectToDb.go      # Database connection setup\n│   ├── loadEnvVariables.go # Environment variables loader\n│   └── syncDb.go           # Database synchronization\n├── middleware/\n│   └── requireAuth.go      # JWT authentication middleware\n└── models/\n    └── userModel.go        # User data model\n```\n\n## 🔧 Installation \u0026 Setup\n\n### Prerequisites\n\n- Go 1.24.5 or higher\n- PostgreSQL database\n- Git\n- CompileDaemon (optional, for development with hot reload)\n  ```bash\n  go install github.com/githubnemo/CompileDaemon@latest\n  ```\n\n### 1. Clone the Repository\n\n```bash\ngit clone https://github.com/noobconner21/go-jwt.git\ncd go-jwt\n```\n\n### 2. Install Dependencies\n\n```bash\ngo mod download\n```\n\n### 3. Environment Configuration\n\nCreate a `.env` file in the root directory:\n\n```env\nDB=postgres://username:password@localhost:5432/database_name?sslmode=disable\nSECRET=your-jwt-secret-key-here\n```\n\n**Environment Variables:**\n\n- `DB`: PostgreSQL connection string\n- `SECRET`: JWT signing secret (use a strong, random string)\n\n### 4. Database Setup\n\nEnsure PostgreSQL is running and create your database:\n\n```sql\nCREATE DATABASE your_database_name;\n```\n\n### 5. Run the Application\n\n#### Production Mode\n\n```bash\ngo run main.go\n```\n\n#### Development Mode (with hot reload)\n\nFor development with automatic recompilation on file changes:\n\n```bash\n# First, build the application\ngo build -o go-jwt\n\n# Then run with CompileDaemon for hot reload\nCompileDaemon --command=\"./go-jwt\"\n```\n\nThe server will start on `http://localhost:8080`\n\n## 📚 API Endpoints\n\n### Authentication Endpoints\n\n#### Register User\n\n```http\nPOST /signup\nContent-Type: application/json\n\n{\n  \"email\": \"user@example.com\",\n  \"password\": \"securepassword\"\n}\n```\n\n**Response:**\n\n- `200 OK` - User created successfully\n- `400 Bad Request` - Invalid request or user already exists\n\n#### Login User\n\n```http\nPOST /login\nContent-Type: application/json\n\n{\n  \"email\": \"user@example.com\",\n  \"password\": \"securepassword\"\n}\n```\n\n**Response:**\n\n- `200 OK` - Login successful (JWT token set as HTTP-only cookie)\n- `400 Bad Request` - Invalid credentials\n\n#### Validate Token\n\n```http\nGET /validate\nCookie: Authorization=jwt-token-here\n```\n\n**Response:**\n\n- `200 OK` - Token valid, returns user information\n- `401 Unauthorized` - Invalid or expired token\n\n## 🔐 Security Features\n\n- **Password Hashing**: Passwords are hashed using bcrypt with salt rounds\n- **JWT Security**: Tokens are signed with HMAC SHA256\n- **HTTP-Only Cookies**: JWT tokens stored securely in HTTP-only cookies\n- **Token Expiration**: Tokens expire after 30 days\n- **Input Validation**: Request body validation and sanitization\n\n## 🏗️ Architecture\n\n### Models\n\n- **User Model**: Defines user structure with email and password fields\n- **GORM Integration**: Automatic table creation and migration\n\n### Controllers\n\n- **Signup**: Handles user registration with password hashing\n- **Login**: Authenticates users and generates JWT tokens\n- **Validate**: Protected endpoint for token validation\n\n### Middleware\n\n- **RequireAuth**: Validates JWT tokens and protects routes\n- **User Context**: Injects authenticated user into request context\n\n### Initializers\n\n- **Database Connection**: PostgreSQL connection with GORM\n- **Environment Loading**: Secure environment variable management\n- **Database Sync**: Automatic table creation and migration\n\n## 🚀 Usage Examples\n\n### Register a new user\n\n```bash\ncurl -X POST http://localhost:8080/signup \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"email\":\"john@example.com\",\"password\":\"mypassword\"}'\n```\n\n### Login\n\n```bash\ncurl -X POST http://localhost:8080/login \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"email\":\"john@example.com\",\"password\":\"mypassword\"}' \\\n  -c cookies.txt\n```\n\n### Access protected route\n\n```bash\ncurl -X GET http://localhost:8080/validate \\\n  -b cookies.txt\n```\n\n## 📝 License\n\nThis project is open source and available under the [MIT License](LICENSE).\n\n\n---\n\n**Built with ❤️ in Go with ShayC**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoobconner21%2Fgo-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoobconner21%2Fgo-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoobconner21%2Fgo-jwt/lists"}