A secure REST API for user authentication built with Express.js, TypeScript, and PostgreSQL.
- 🔐 User registration with bcrypt password hashing
- 🔑 JWT-based authentication
- 👤 Protected routes with token verification
- 📊 PostgreSQL database integration
- 🛡️ Type-safe with TypeScript
- Runtime: Node.js
- Framework: Express.js
- Language: TypeScript
- Database: PostgreSQL
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcrypt
- Node.js (v18 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
- Clone the repository
git clone https://github.com/Daudsaid/auth-api-typescript.git
cd auth-api-typescript- Install dependencies
npm install- Create PostgreSQL database
psql -U your_username -d postgres
CREATE DATABASE auth_api_ts;
\c auth_api_ts
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- Configure environment variables
Create a .env file in the root directory:
PORT=3001
JWT_SECRET=your_super_secret_key_here
DATABASE_URL=postgresql://username@localhost:5432/auth_api_ts- Run the development server
npm run devThe API will be available at http://localhost:3001
POST /api/auth/register
Content-Type: application/json
{
"username": "johndoe",
"email": "john@example.com",
"password": "securepassword123"
}Response (201):
{
"message": "User registered successfully",
"user": {
"id": 1,
"username": "johndoe",
"email": "john@example.com",
"created_at": "2026-01-12T15:30:00.000Z"
}
}POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword123"
}Response (200):
{
"message": "Login successful",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"username": "johndoe",
"email": "john@example.com"
}
}GET /api/auth/profile
Authorization: Bearer <your_jwt_token>Response (200):
{
"user": {
"id": 1,
"username": "johndoe",
"email": "john@example.com",
"created_at": "2026-01-12T15:30:00.000Z"
}
}auth-api-typescript/
├── src/
│ ├── server.ts # Express app setup
│ ├── db.ts # PostgreSQL connection
│ ├── types.ts # TypeScript interfaces
│ ├── authRoutes.ts # Authentication routes
│ └── authMiddleware.ts # JWT verification middleware
├── .env # Environment variables (not in git)
├── .gitignore
├── package.json
├── tsconfig.json # TypeScript configuration
└── README.md
# Development with hot reload
npm run dev
# Build for production
npm run build
# Start production server
npm start- ✅ Passwords hashed with bcrypt (10 salt rounds)
- ✅ JWT tokens with 24-hour expiration
- ✅ Protected routes require valid tokens
- ✅ SQL injection protection with parameterized queries
- ✅ Environment variables for sensitive data
The API returns appropriate HTTP status codes:
200- Success201- Created400- Bad Request (user already exists)401- Unauthorized (invalid credentials/token)403- Forbidden (no token provided)404- Not Found500- Server Error
- Email verification
- Password reset functionality
- Refresh tokens
- User roles and permissions
- Rate limiting
- Input validation with Zod
- Unit and integration tests
- Docker containerization
MIT
Daud Abdi
- GitHub: @Daudsaid
- LinkedIn: daudabdi0506
- Email: daudsaidabdi@gmail.com