Skip to content

aelaraby6/Authify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Authify

A comprehensive authentication module built with Express.js, TypeScript, and React, providing secure user authentication with multiple features including OAuth integration, 2FA, and password management.

use-case

✨ Features

Core Authentication

  • User Registration & Login - Secure signup and login with email/password
  • Session Management - JWT-based authentication with refresh tokens
  • OAuth Integration - Sign in with GitHub and Google
  • Role-Based Access Control - User roles and permissions management

Security Features

  • Two-Factor Authentication (2FA) - TOTP-based 2FA with QR code generation
  • Password Management
    • Forgot password with OTP verification
    • Password reset functionality
    • Update password for authenticated users
  • Rate Limiting - Protection against brute force attacks
  • Secure Cookies - HttpOnly, Secure, and SameSite cookie configurations
  • Password Hashing - Bcrypt-based password encryption
  • Input Validation - Request validation using schemas

Additional Features

  • API Documentation - Swagger/OpenAPI documentation at /docs
  • Error Handling - Comprehensive global error handling
  • CORS Configuration - Secure cross-origin resource sharing
  • Helmet Integration - Enhanced API security headers
password-reset

πŸ›  Technology Stack

Backend

  • Runtime: Node.js with TypeScript
  • Framework: Express.js
  • Authentication: Passport.js (Local, GitHub, Google strategies)
  • Session Management: express-session
  • Token Management: jsonwebtoken
  • 2FA: speakeasy, qrcode
  • Validation: Custom validation middleware
  • Security: helmet, bcrypt, rate-limiter
  • Database: MongoDB (assumed from User model)

Frontend

  • Framework: React 18
  • Routing: React Router v6
  • Language: TypeScript
  • UI Components: Custom components

πŸ— Architecture

The project follows a modular, layered architecture with clear separation of concerns:

πŸ“ Project Structure

authify/
β”œβ”€β”€ Server/                              # Backend Application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ controllers/                 # Request handlers
β”‚   β”‚   β”‚   └── Auth/
β”‚   β”‚   β”‚       └── auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ routers/                     # API routes
β”‚   β”‚   β”‚   └── auth.router.ts
β”‚   β”‚   β”œβ”€β”€ middlewares/                 # Custom middleware
β”‚   β”‚   β”‚   β”œβ”€β”€ authenticate_session.middleware.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ authenticate_token.middleware.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ rate_limiter.middleware.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ validate.middleware.ts
β”‚   β”‚   β”‚   └── global_error_handler.middleware.ts
β”‚   β”‚   β”œβ”€β”€ models/                      # Database models
β”‚   β”‚   β”‚   └── User/
β”‚   β”‚   β”‚       └── user.model.ts
β”‚   β”‚   β”œβ”€β”€ services/                    # Business logic services
β”‚   β”‚   β”‚   β”œβ”€β”€ jwt.service.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ otp.service.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ email.service.ts
β”‚   β”‚   β”‚   └── password.service.ts
β”‚   β”‚   β”œβ”€β”€ validations/                 # Request validation schemas
β”‚   β”‚   β”‚   └── Auth/
β”‚   β”‚   β”‚       └── auth.validation.ts
β”‚   β”‚   β”œβ”€β”€ config/                      # Configuration files
β”‚   β”‚   β”‚   β”œβ”€β”€ corsOptions.ts
β”‚   β”‚   β”‚   └── passportConfig.ts
β”‚   β”‚   β”œβ”€β”€ Errors/                      # Custom error classes
β”‚   β”‚   β”‚   └── error.ts
β”‚   β”‚   β”œβ”€β”€ helper/                      # Helper utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ calcTime.ts
β”‚   β”‚   β”‚   └── sessionhelper.ts
β”‚   β”‚   β”œβ”€β”€ types/                       # TypeScript types
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”œβ”€β”€ utils/                       # Utility functions
β”‚   β”‚   β”‚   └── swagger.ts
β”‚   β”‚   β”œβ”€β”€ docs/                        # API documentation
β”‚   β”‚   β”œβ”€β”€ app.ts                       # Express app configuration
β”‚   β”‚   └── server.ts                    # Server entry point
β”‚   β”œβ”€β”€ scripts/                         # Build/deployment scripts
β”‚   β”œβ”€β”€ .gitignore
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   β”œβ”€β”€ package.json
β”‚   └── package-lock.json
β”‚
β”œβ”€β”€ Client/                              # Frontend Application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ Pages/                       # Page components
β”‚   β”‚   β”‚   β”œβ”€β”€ Login.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Signup.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ForgotPasswordPage.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ OTP.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ResetPassword.tsx
β”‚   β”‚   β”‚   └── Dashboard.tsx
β”‚   β”‚   β”œβ”€β”€ components/                  # Reusable components
β”‚   β”‚   β”œβ”€β”€ assets/                      # Static assets
β”‚   β”‚   β”œβ”€β”€ core/                        # Core application logic
β”‚   β”‚   β”‚   β”œβ”€β”€ Storage/
β”‚   β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ constants/
β”‚   β”‚   β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── utils/
β”‚   β”‚   β”œβ”€β”€ router/                      # Application routing
β”‚   β”‚   β”‚   └── AppRouter.tsx
β”‚   β”‚   β”œβ”€β”€ App.tsx                      # Root component
β”‚   β”‚   β”œβ”€β”€ App.css                      # Global styles
β”‚   β”‚   β”œβ”€β”€ main.tsx                     # Application entry point
β”‚   β”‚   └── index.css                    # Base styles
β”‚   β”œβ”€β”€ public/                          # Public assets
β”‚   β”œβ”€β”€ .gitignore
β”‚   β”œβ”€β”€ components.json                  # Component configuration
β”‚   β”œβ”€β”€ eslint.config.js
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ package-lock.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   β”œβ”€β”€ tsconfig.app.json
β”‚   β”œβ”€β”€ tsconfig.node.json
β”‚   └── vite.config.ts
β”‚
└── README.md                            # Project documentation

πŸš€ Installation

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB
  • npm or yarn

Backend Setup

# Navigate to server directory
cd Server

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your configuration

# Run development server
npm run dev

# Build for production
npm run build

# Start production server
npm start

Frontend Setup

# Navigate to client directory
cd Client

# Install dependencies
npm install

# Run development server
npm run dev

# Build for production
npm run build

πŸ” Environment Variables

Create a .env file in the Server directory:

# Server Configuration
NODE_ENV=development
PORT=3000

# Database
MONGODB_URI=mongodb://localhost:27017/authify

# JWT Secrets
ACCESS_TOKEN_SECRET=your_access_token_secret
REFRESH_TOKEN_SECRET=your_refresh_token_secret

# Session
SESSION_SECRET=your_session_secret

# GitHub OAuth
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
GITHUB_CALLBACK_URL=http://localhost:3000/api/auth/github/callback

# Google OAuth
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_CALLBACK_URL=http://localhost:3000/api/auth/google/callback

# Email Service (for OTP)
EMAIL_SERVICE=gmail
EMAIL_USER=your_email@gmail.com
EMAIL_PASSWORD=your_email_password

# Frontend URL
FRONTEND_URL=http://localhost:5173

πŸ“š API Documentation

API documentation is available at /docs endpoint using Swagger UI.

swagger

πŸ”„ Authentication Flow

1. Standard Email/Password Flow

User Registration:
1. User submits signup form (name, email, password)
2. Server validates input and checks for existing user
3. Password is hashed using bcrypt
4. User document is created in database
5. JWT tokens (access & refresh) are generated
6. Refresh token stored in httpOnly cookie
7. Access token returned in response

User Login:
1. User submits login credentials
2. Passport local strategy validates credentials
3. Session is established via req.login()
4. JWT tokens generated
5. Tokens sent to client

2. OAuth Flow (GitHub/Google)

1. User clicks "Sign in with GitHub/Google"
2. User redirected to OAuth provider
3. User authorizes application
4. Provider redirects to callback URL with code
5. Server exchanges code for access token
6. User profile fetched from provider
7. User created/updated in database
8. JWT tokens generated
9. User redirected to frontend dashboard with token

3. Password Reset Flow

1. User requests password reset (forgot-password)
2. OTP generated and sent via email
3. User submits OTP for verification
4. If valid, user can set new password
5. Password updated and OTP cleared

4. Two-Factor Authentication Flow

Setup:
1. User enables 2FA from dashboard
2. Server generates TOTP secret
3. QR code generated for authenticator app
4. User scans QR code
5. Secret saved to user profile

Login with 2FA:
1. User completes standard login
2. System prompts for 2FA token
3. User enters token from authenticator app
4. Server verifies token using speakeasy
5. Access granted if valid

πŸ§ͺ Testing

The React application serves as a testing environment for the authentication module. It includes forms for all authentication features with proper error handling and user feedback.

πŸ‘₯ Contributors

Thanks goes to these wonderful people in the team:


Abdelrahman Elaraby

Ahmed Ali

About

Authentication module built with Express, MongoDB, and JWT

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •