A robust TypeScript-based authentication API built with Node.js and Express, providing secure user authentication and authorization functionality.
- User Registration & Login - Secure user account creation and authentication
- JWT Token Authentication - Stateless authentication using JSON Web Tokens
- Password Hashing - Secure password storage using bcrypt
- Input Validation - Comprehensive request validation and sanitization
- Error Handling - Centralized error handling with detailed logging
- TypeScript Support - Full TypeScript implementation for type safety
- Environment Configuration - Configurable environment variables
Before running this project, make sure you have the following installed:
- Node.js (v14 or higher)
- npm or yarn
- MongoDB or your preferred database
- Clone the repository:
git clone https://github.com/sachinggsingh/ts-auth-api.git
cd ts-auth-api
- Install dependencies:
npm install
- Create a
.env
file in the root directory and add your environment variables:
PORT=3000
DATABASE_URL=your_database_connection_string
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=7d
BCRYPT_SALT_ROUNDS=12
- Build the TypeScript code:
npm run build
- Start the development server:
npm run dev
Method | Endpoint | Description | Body |
---|---|---|---|
POST | /api/auth/register |
Register a new user | { email, password, name } |
POST | /api/auth/login |
Login user | { email, password } |
POST | /api/auth/logout |
Logout user | - |
GET | /api/auth/me |
Get current user | - |
Method | Endpoint | Description | Headers |
---|---|---|---|
GET | /api/users/profile |
Get user profile | Authorization: Bearer <token> |
PUT | /api/users/profile |
Update user profile | Authorization: Bearer <token> |
npm run dev
- Start development server with hot reloadnpm run build
- Build TypeScript to JavaScriptnpm start
- Start production servernpm run test
- Run testsnpm run lint
- Run ESLintnpm run format
- Format code with Prettier
ts-auth-api/
├── src/
│ ├── controllers/ # Route controllers
│ ├── middleware/ # Custom middleware
│ ├── models/ # Database models
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions
│ └── app.ts # Express app configuration
├── dist/ # Compiled JavaScript (generated)
├── logs/ # Application logs
├── .env.example # Environment variables template
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
- Password Hashing: Uses bcrypt for secure password storage
- JWT Authentication: Stateless authentication with configurable expiration
- Input Validation: Validates and sanitizes all user inputs
- Rate Limiting: Prevents brute force attacks
- CORS Configuration: Configurable cross-origin resource sharing
- Helmet Integration: Sets various HTTP headers for security
Run the test suite:
npm test
For test coverage:
npm run test:coverage
Create a .env
file based on .env.example
:
Variable | Description | Default |
---|---|---|
PORT |
Server port | 3000 |
DATABASE_URL |
Database connection string | - |
JWT_SECRET |
JWT signing secret | - |
JWT_EXPIRES_IN |
JWT expiration time | 7d |
BCRYPT_SALT_ROUNDS |
Bcrypt salt rounds | 12 |
NODE_ENV |
Environment mode | development |
- Build the Docker image:
docker build -t ts-auth-api .
- Run the container:
docker run -p 3000:3000 --env-file .env ts-auth-api
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Sachin Singh - sachinggsingh
- Express.js team for the excellent web framework
- TypeScript team for bringing type safety to JavaScript
- All contributors who help improve this project
If you have any questions or need help, please:
- Check the Issues page
- Create a new issue if your question isn't already addressed
- Reach out via email or social media
⭐ Star this repository if you find it helpful!
This README.md file provides comprehensive documentation for your TypeScript authentication API project [^1][^3]. It includes all the essential sections that make it easy for developers to understand, install, and contribute to your project.