Skip to content

Daudsaid/authentication-authorization-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Authentication & Authorization API

Built by Daud Abdi

🌐 Live Demo: http://3.10.174.145:4000
πŸ’» GitHub: View Source
πŸ“§ Contact: daudsaidabdi@gmail.com
πŸ“± Portfolio: daud-abdi-portfolio-site.vercel.app


A production-ready authentication and authorization API built with Node.js, Express, TypeScript, and PostgreSQL. Deployed on AWS infrastructure with JWT-based authentication, email verification, and role-based access control.

✨ Features

  • πŸ” User registration with email verification
  • 🎫 JWT-based authentication (access + refresh tokens)
  • πŸ”„ Automatic token refresh mechanism
  • πŸ”‘ Secure password reset flow
  • πŸ‘₯ Role-based access control (user/admin)
  • πŸ“§ Email notifications (verification, password reset)
  • πŸ›‘οΈ bcrypt password hashing
  • βœ… Input validation and error handling
  • πŸ§ͺ Comprehensive test coverage with Jest

πŸ› οΈ Tech Stack

Backend:

  • Node.js
  • Express.js
  • TypeScript
  • PostgreSQL
  • JWT (jsonwebtoken)
  • bcrypt

Deployment:

  • AWS EC2 (Ubuntu 24.04)
  • AWS RDS (PostgreSQL)
  • PM2 Process Manager
  • SSL/TLS Database Connection

Testing:

  • Jest
  • Supertest

πŸ“‘ API Endpoints

Public Endpoints

Register User

POST http://3.10.174.145:4000/api/auth/register
Content-Type: application/json

{
  "username": "johndoe",
  "email": "john@example.com",
  "password": "SecurePass123!"
}

Login

POST http://3.10.174.145:4000/api/auth/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "SecurePass123!"
}

Refresh Token

POST http://3.10.174.145:4000/api/auth/refresh
Content-Type: application/json

{
  "refreshToken": "your_refresh_token_here"
}

Forgot Password

POST http://3.10.174.145:4000/api/auth/forgot-password
Content-Type: application/json

{
  "email": "john@example.com"
}

Protected Endpoints

Get User Profile

GET http://3.10.174.145:4000/api/auth/profile
Authorization: Bearer your_access_token_here

Utility Endpoints

Health Check

GET http://3.10.174.145:4000/health

API Info

GET http://3.10.174.145:4000/

πŸš€ Deployment Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     AWS EC2 (Ubuntu 24.04)      β”‚
β”‚   Node.js + Express + PM2       β”‚
β”‚   Port: 4000                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
              β”‚ SSL Connection
              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   AWS RDS PostgreSQL 17.6       β”‚
β”‚   - users table                 β”‚
β”‚   - refresh_tokens table        β”‚
β”‚   - password_reset_tokens       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’» Local Development Setup

Prerequisites

  • Node.js 18+
  • PostgreSQL 14+
  • npm or yarn

Installation

  1. Clone the repository
git clone https://github.com/Daudsaid/authentication-authorization-api.git
cd authentication-authorization-api
  1. Install dependencies
npm install
  1. Set up environment variables

Create a .env file:

PORT=4000
DATABASE_URL=postgresql://username:password@localhost:5432/auth_db
JWT_ACCESS_SECRET=your_access_secret_here
JWT_REFRESH_SECRET=your_refresh_secret_here
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_app_password
CLIENT_URL=http://localhost:3000
NODE_ENV=development
  1. Set up database
# Create database
createdb auth_db

# Run schema (see src/__tests__/setup/testDb.helper.ts for table definitions)
  1. Run development server
npm run dev

The API will be available at http://localhost:4000

πŸ§ͺ Testing

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

πŸ“¦ Production Build

# Build TypeScript to JavaScript
npm run build

# Start production server
npm start

πŸ”’ Security Features

  • βœ… Password hashing with bcrypt (10 rounds)
  • βœ… JWT tokens with expiration
  • βœ… Refresh token rotation
  • βœ… HTTPS/SSL database connections
  • βœ… Environment variable management
  • βœ… Input validation and sanitization
  • βœ… SQL injection protection (parameterized queries)
  • βœ… CORS configuration

πŸ“Š Database Schema

users
β”œβ”€β”€ id (SERIAL PRIMARY KEY)
β”œβ”€β”€ username (VARCHAR UNIQUE)
β”œβ”€β”€ email (VARCHAR UNIQUE)
β”œβ”€β”€ password (VARCHAR - hashed)
β”œβ”€β”€ role (VARCHAR - 'user'/'admin')
β”œβ”€β”€ is_verified (BOOLEAN)
β”œβ”€β”€ verification_token (VARCHAR)
β”œβ”€β”€ created_at (TIMESTAMP)
└── updated_at (TIMESTAMP)

refresh_tokens
β”œβ”€β”€ id (SERIAL PRIMARY KEY)
β”œβ”€β”€ user_id (INTEGER FK β†’ users.id)
β”œβ”€β”€ token (VARCHAR UNIQUE)
β”œβ”€β”€ expires_at (TIMESTAMP)
└── created_at (TIMESTAMP)

password_reset_tokens
β”œβ”€β”€ id (SERIAL PRIMARY KEY)
β”œβ”€β”€ user_id (INTEGER FK β†’ users.id)
β”œβ”€β”€ token (VARCHAR UNIQUE)
β”œβ”€β”€ expires_at (TIMESTAMP)
β”œβ”€β”€ used (BOOLEAN)
└── created_at (TIMESTAMP)

🎯 Future Enhancements

  • OAuth 2.0 integration (Google, GitHub)
  • Two-factor authentication (2FA)
  • Rate limiting middleware
  • API documentation with Swagger/OpenAPI
  • Docker containerization
  • CI/CD pipeline with GitHub Actions

πŸ“„ License

MIT License

πŸ‘€ Author

Daud Abdi

πŸ™ Acknowledgments

  • Built as part of my full-stack development portfolio
  • Deployed on AWS to demonstrate cloud infrastructure skills
  • Following industry best practices for authentication systems

⭐ If you found this project helpful, please consider giving it a star!

Made with ❀️ by Daud Abdi

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages