Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express TypeScript Modular Auth API

A modular, production-ready Express.js API using TypeScript, MySQL, and Joi for authentication workflows (login, register, forgot password, OTP, etc.).

Features

  • Modular folder structure (per-feature modules)
  • TypeScript for type safety
  • MySQL database integration (via mysql2)
  • Secure password hashing with bcryptjs
  • OTP generation and validation
  • Request validation with Joi middleware
  • Common response and logger utilities

Project Structure

template-js/
├── src/
│   ├── app.ts
│   ├── server.ts
│   ├── config/
│   │   ├── db.ts
│   │   └── env.ts
│   ├── common/
│   │   ├── logger.ts
│   │   ├── response.ts
│   │   └── validate.ts
│   ├── modules/
│   │   ├── auth/
│   │   │   ├── auth.controller.ts
│   │   │   ├── auth.model.ts
│   │   │   ├── auth.routes.ts
│   │   │   └── auth.service.ts
│   │   └── user/ ...
│   └── utils/ ...
├── package.json
├── tsconfig.json
└── README.md

Setup

  1. Install dependencies:
    npm install
  2. Configure environment variables:
    • Create a .env file or set these variables:
      • DB_HOST (default: localhost)
      • DB_USER (default: root)
      • DB_PASS (default: empty)
      • DB_NAME (default: app_db)
      • PORT (default: 3000)
  3. Start MySQL server and ensure the database exists (the users table is auto-created).
  4. Run the app:
    • Development: npm run dev
    • Production: npm run build && npm start

API Endpoints

All endpoints are prefixed with /api/auth.

Auth

  • POST /api/auth/login
    • Body: { "username": string, "password": string }
  • POST /api/auth/register
    • Body: { "username": string, "password": string, "email": string }
  • POST /api/auth/forgot
    • Body: { "email": string }
  • POST /api/auth/send-otp
    • Body: { "email": string }
  • POST /api/auth/validate-otp
    • Body: { "email": string, "otp": string }

Response Format

All responses use a common format:

{
  status: 'success' | 'error',
  message: string,
  data?: any,
  code?: number
}

Validation

All request bodies are validated with Joi. Invalid requests return a 400 error with details.

Extending

  • Add new modules in src/modules/ (copy the auth pattern)
  • Add new routes, controllers, and services as needed

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages