Skip to content

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.

License

Notifications You must be signed in to change notification settings

AchrefDevTN/jwt-auth-starter

Repository files navigation

JWT Authentication Starter

JWT Authentication

Overview

This 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.

Table of Contents

Features

  • User Registration: Secure user registration with bcrypt password hashing.
  • User Login: JWT token generation for authenticated users.
  • Protected Routes: Middleware-enforced JWT protection for sensitive routes.
  • Error Handling: Centralized error management with detailed logging using Winston.

Tech Stack

  • Backend: Node.js, Express
  • Database: MySQL
  • Authentication: JWT (JSON Web Tokens)
  • Logging: Winston
  • Environment Management: dotenv

Prerequisites

Ensure you have the following installed and configured on your machine:

For installation assistance, refer to the official documentation or guides.

Setup

  1. Clone the repository:

    git clone https://github.com/AchrefDevTN/jwt-auth-starter.git
  2. Navigate into the project directory:

    cd jwt-auth-starter
  3. Install dependencies:

    npm install
  4. Create a .env file in the root directory: (Copy the contents below and configure your environment variables)

    PORT=5000
    JWT_SECRET=your_jwt_secret
    JWT_EXPIRATION=1h
    DB_USERNAME=root
    DB_PASSWORD=your_db_password
    DB_NAME=jwt_auth_db
    DB_HOST=localhost
  5. Run the application:

    • For development
    npm run dev
    • For production
    npm start
  6. Access the API:

    • The server will be running on
    http://localhost:5000

API Endpoints

  • POST /api/auth/register
    Registers a new user.
    Request body:

    {
      "username": "string",
      "password": "string"
    }

    Response:

    {
       "message": "User registered successfully"
    }
  • POST /api/auth/login
    Logs in a user and returns a JWT token.
    Request body:

    {
      "username": "string",
      "password": "string"
    }

    Response:

    {
      "token": "jwt_token_string"
    }
  • GET /api/auth/protected
    Access a protected route (JWT required in the Authorization header).
    Response:

    {
      "message": "This is a protected route",
      "userId": "user_id"
    }

Error Handling

Errors 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.

Logging

The application uses Winston for logging. Logs are written to both the console and a file named app.log.

Contributing

If you have suggestions or improvements, please submit an issue or pull request. Contributions are welcome!

License

This project is licensed under the MIT License. See the LICENSE file for details.

🔗 Links

linkedin

Node.js Express MySQL License
Node.js Express MySQL License

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published