{"id":21525090,"url":"https://github.com/achrefdevtn/jwt-auth-starter","last_synced_at":"2026-04-09T17:13:39.218Z","repository":{"id":255940743,"uuid":"844975323","full_name":"AchrefDevTN/jwt-auth-starter","owner":"AchrefDevTN","description":"A Node.js and Express application showcasing JWT authentication with MySQL. Includes secure user registration, login, token-based protection for routes, and centralized error handling with Winston. Perfect for learning and implementing JWT in a real-world setting.","archived":false,"fork":false,"pushed_at":"2024-09-08T00:41:51.000Z","size":42,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T18:18:20.677Z","etag":null,"topics":["api-security","authentication","expressjs","jwt","mysql","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AchrefDevTN.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-08-20T10:37:23.000Z","updated_at":"2024-09-08T00:41:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"18b198e3-48cd-411b-9352-73ea5f01b997","html_url":"https://github.com/AchrefDevTN/jwt-auth-starter","commit_stats":null,"previous_names":["achrefdevtn/jwt-auth-starter"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AchrefDevTN/jwt-auth-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AchrefDevTN%2Fjwt-auth-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AchrefDevTN%2Fjwt-auth-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AchrefDevTN%2Fjwt-auth-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AchrefDevTN%2Fjwt-auth-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AchrefDevTN","download_url":"https://codeload.github.com/AchrefDevTN/jwt-auth-starter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AchrefDevTN%2Fjwt-auth-starter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28132549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-30T02:00:05.476Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["api-security","authentication","expressjs","jwt","mysql","nodejs"],"created_at":"2024-11-24T01:32:30.043Z","updated_at":"2025-12-30T21:04:43.732Z","avatar_url":"https://github.com/AchrefDevTN.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JWT Authentication Starter\r\n\r\n![JWT Authentication](https://miro.medium.com/v2/resize:fit:4800/format:webp/1*dm00QProOdGxcV9gv0RX_w.png)\r\n\r\n## Overview\r\n\r\nThis repository provides a comprehensive implementation of JWT authentication using Node.js, Express, and MySQL. The project showcases best practices in user registration, login, token-based authentication, and protected routes, all while maintaining robust error handling and logging mechanisms.\r\n\r\n## Table of Contents\r\n\r\n- [Features](#features)\r\n- [Tech Stack](#tech-stack)\r\n- [Prerequisites](#prerequisites)\r\n- [Setup](#setup)\r\n- [API Endpoints](#api-endpoints)\r\n- [Error Handling](#error-handling)\r\n- [Logging](#logging)\r\n- [Contributing](#contributing)\r\n- [License](#license)\r\n- [Links](#-links)\r\n\r\n## Features\r\n\r\n- **User Registration**: Secure user registration with bcrypt password hashing.\r\n- **User Login**: JWT token generation for authenticated users.\r\n- **Protected Routes**: Middleware-enforced JWT protection for sensitive routes.\r\n- **Error Handling**: Centralized error management with detailed logging using Winston.\r\n\r\n## Tech Stack\r\n\r\n- **Backend**: Node.js, Express\r\n- **Database**: MySQL\r\n- **Authentication**: JWT (JSON Web Tokens)\r\n- **Logging**: Winston\r\n- **Environment Management**: dotenv\r\n\r\n## Prerequisites\r\n\r\nEnsure you have the following installed and configured on your machine:\r\n\r\n- **Node.js**: Version 14 or higher. [Download here](https://nodejs.org/).\r\n- **MySQL**: MySQL server or a compatible database. [Download here](https://dev.mysql.com/downloads/mysql/).\r\n\r\nFor installation assistance, refer to the official documentation or guides.\r\n\r\n## Setup\r\n\r\n1. **Clone the repository:**\r\n   ```bash\r\n   git clone https://github.com/AchrefDevTN/jwt-auth-starter.git\r\n   ```\r\n2. **Navigate into the project directory:**\r\n   ```bash\r\n   cd jwt-auth-starter\r\n   ```\r\n  \r\n3. **Install dependencies:**\r\n   ```bash\r\n   npm install\r\n   ```\r\n4. **Create a .env file in the root directory:**\r\n   (Copy the contents below and configure your environment variables)\r\n  \r\n   ```ini\r\n   PORT=5000\r\n   JWT_SECRET=your_jwt_secret\r\n   JWT_EXPIRATION=1h\r\n   DB_USERNAME=root\r\n   DB_PASSWORD=your_db_password\r\n   DB_NAME=jwt_auth_db\r\n   DB_HOST=localhost\r\n   ```\r\n\r\n5. **Run the application:**\r\n   - For development\r\n   ```bash\r\n   npm run dev\r\n   ```\r\n   - For production\r\n   ```bash\r\n   npm start\r\n   ```\r\n\r\n6. **Access the API:**\r\n   - The server will be running on\r\n   ```bash\r\n   http://localhost:5000\r\n   ```\r\n\r\n\r\n\r\n## API Endpoints\r\n\r\n- **POST `/api/auth/register`**  \r\n  Registers a new user.  \r\n  **Request body:**  \r\n  ```json\r\n  {\r\n    \"username\": \"string\",\r\n    \"password\": \"string\"\r\n  }\r\n  ```\r\n  **Response:**  \r\n  ```json\r\n  {\r\n     \"message\": \"User registered successfully\"\r\n  }\r\n  ```\r\n  \r\n- **POST `/api/auth/login`**  \r\n  Logs in a user and returns a JWT token.  \r\n  **Request body:**  \r\n  ```json\r\n\r\n  {\r\n    \"username\": \"string\",\r\n    \"password\": \"string\"\r\n  }\r\n  ```\r\n\r\n  **Response:**  \r\n  ```json\r\n\r\n  {\r\n    \"token\": \"jwt_token_string\"\r\n  }\r\n  ```\r\n\r\n- **GET `/api/auth/protected`**  \r\n  Access a protected route (JWT required in the Authorization header).  \r\n  **Response:**      \r\n  ```json\r\n  {\r\n    \"message\": \"This is a protected route\",\r\n    \"userId\": \"user_id\"\r\n  }\r\n  ```\r\n\r\n\r\n## Error Handling\r\nErrors are handled centrally in the middleware. If an error occurs, the server responds with an appropriate status code and message. All errors are logged using Winston for debugging and monitoring purposes.\r\n\r\n## Logging\r\nThe application uses Winston for logging. Logs are written to both the console and a file named \u003ckbd\u003eapp.log\u003c/kbd\u003e.\r\n\r\n## Contributing\r\nIf you have suggestions or improvements, please submit an issue or pull request. Contributions are welcome!\r\n\r\n## License\r\nThis project is licensed under the MIT License. See the [LICENSE](https://github.com/AchrefDevTN/jwt-auth-starter/blob/main/LICENSE) file for details.\r\n\r\n## 🔗 Links\r\n[![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white)]([https://www.linkedin.com/](https://www.linkedin.com/in/achref-ben-brahim-dev/))\r\n\r\n\r\n| Node.js | Express | MySQL | License |\r\n|---------|---------|-------|---------|\r\n| [![Node.js](https://img.shields.io/badge/Node.js-v14%2B-green)](https://nodejs.org/) | [![Express](https://img.shields.io/badge/Express-v4.18.2-blue)](https://expressjs.com/) | [![MySQL](https://img.shields.io/badge/MySQL-v8.0%2B-blue)](https://www.mysql.com/) | [![License](https://img.shields.io/badge/license-MIT-brightgreen)](https://github.com/AchrefDevTN/jwt-auth-starter/blob/main/LICENSE) |\r\n\r\n\r\n\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fachrefdevtn%2Fjwt-auth-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fachrefdevtn%2Fjwt-auth-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fachrefdevtn%2Fjwt-auth-starter/lists"}