Skip to content

mrXrobot26/ExpreesTemplateWithSQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Express.js Template with MySQL and Authentication

A robust, production-ready Express.js starter template with MySQL integration using Prisma ORM, featuring comprehensive user authentication, email functionality, and file upload capabilities.

✨ Features

  • πŸ” User Authentication

    • JWT-based secure authentication
    • Role-based authorization (User/Admin)
    • Protected routes with middleware
    • Session management with cookies
  • πŸ“§ Email Integration

    • Gmail SMTP integration
    • Beautiful HTML email templates
    • Secure password reset workflow
    • Transactional email support
  • πŸ“ File Management

    • User avatar uploads
    • Secure file storage
    • Automatic file cleanup
    • Default avatar support
  • πŸ›‘οΈ Security

    • Role-based access control (RBAC)
    • Input validation
    • Password hashing with bcrypt
    • Secure reset code generation
  • πŸ’Ύ Database

    • MySQL with Prisma ORM
    • Automated migrations
    • Type-safe database queries
    • Efficient connection pooling
  • βš™οΈ Development Tools

    • Jest testing setup
    • Environment configuration
    • API error handling
    • Standardized response format

πŸ“‚ Project Structure

β”œβ”€β”€ config/               # Configuration files
β”œβ”€β”€ controller/
β”‚   β”œβ”€β”€ authController.js    # Authentication logic
β”‚   β”œβ”€β”€ userController.js    # User management
β”‚   └── forgetPasswordController.js
β”œβ”€β”€ Middleware/
β”‚   β”œβ”€β”€ authMiddleware.js    # JWT authentication
β”‚   └── validationMiddleware.js
β”œβ”€β”€ prisma/
β”‚   β”œβ”€β”€ schema.prisma        # Database schema
β”‚   └── migrations/         # Database migrations
β”œβ”€β”€ router/
β”‚   β”œβ”€β”€ authRouter.js
β”‚   β”œβ”€β”€ userRouter.js
β”‚   └── forgetPasswordRouter.js
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ APIError.js         # Error handling
β”‚   β”œβ”€β”€ APIResponse.js      # Response formatting
β”‚   └── sendMail.js        # Email utility
β”œβ”€β”€ uploads/
β”‚   └── userAvatar/        # User avatar storage
└── tests/                # Jest test files

πŸ”§ Prerequisites

  • Node.js (v14 or higher)
  • MySQL Server (v5.7 or higher)
  • npm or yarn

πŸš€ Getting Started

1. Clone the repository

git clone <https://github.com/mrXrobot26/ExpreesTemplateWithSQL>
cd ExpreesTemplateWithSQL

2. Install dependencies

npm install

3. Configure environment variables

Create a .env file in the root directory:

# Application
NODE_ENV=development
PORT=3000

# Database
DATABASE_URL="mysql://username:password@localhost:3306/your_database"

# Authentication
JWT_SECRET=your_jwt_secret_key

# Email Configuration (Gmail)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your_email@gmail.com
EMAIL_PASSWORD=your_app_specific_password

4. Set up the database

# Create database tables
npx prisma migrate dev

5. Start the development server

npm run dev

Your API will be available at http://localhost:3000/api/v1

πŸ“‘ API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /api/v1/auth/register Register a new user No
POST /api/v1/auth/login User login No

Password Management

Method Endpoint Description Auth Required
POST /api/v1/forget-password Request password reset No
POST /api/v1/forget-password/verify-code Verify reset code No
POST /api/v1/forget-password/reset-password Set new password No

User Management

Method Endpoint Description Auth Required
GET /api/v1/users Get all users Admin
GET /api/v1/users/:id Get user by ID Yes*
PUT /api/v1/users/:id Update user Yes*
DELETE /api/v1/users/:id Delete user Yes*
PATCH /api/v1/users/:id/avatar Update user avatar Yes*

* Users can only access their own resources unless they have admin privileges

πŸ”’ Authentication Flow

Registration

  1. Client sends POST request to /api/v1/auth/register with:

    {
      "name": "John Doe",
      "email": "john@example.com",
      "password": "securePassword123",
      "passwordConfirm": "securePassword123"
    }
  2. Server validates input, hashes password, and creates user

  3. Server returns JWT token and user data

Login

  1. Client sends POST request to /api/v1/auth/login with:

    {
      "email": "john@example.com",
      "password": "securePassword123"
    }
  2. Server validates credentials and issues JWT token

  3. Token is returned in response and set as HTTP-only cookie

Password Reset Flow

  1. Request reset code:

    POST /api/v1/forget-password
    {
      "email": "user@example.com"
    }
  2. Verify reset code:

    POST /api/v1/forget-password/verify-code
    {
      "email": "user@example.com",
      "resetCode": "123456"
    }
  3. Set new password:

    POST /api/v1/forget-password/reset-password
    {
      "email": "user@example.com",
      "newPassword": "newSecurePassword123"
    }

πŸ“§ Email Setup

Using Gmail

  1. Enable 2-Step Verification in your Google Account
  2. Generate an App Password:
    • Go to Google Account Settings β†’ Security β†’ 2-Step Verification β†’ App passwords
    • Select "Mail" and "Other" (name it "Express App")
    • Use the 16-character password in your .env file

HTML Email Templates

The system includes pre-built HTML email templates for:

  • Password reset codes

πŸ—ƒοΈ Database Schema

User Model

model User {
  id                   Int       @id @default(autoincrement())
  createdAt            DateTime  @default(now())
  email                String    @unique
  name                 String?
  role                 Role      @default(USER)
  password             String
  avatar               String    @default("avatar.png")
  passwordResetCode    String?
  passwordResetExpires DateTime?
  passwordResetVerify  Boolean   @default(false)
}

enum Role {
  USER
  ADMIN
}

πŸ§ͺ Testing

# Run all tests
npm test

# Run specific test suite
npm test -- --testPathPattern=auth

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published