0.1.12 • Published 10 months ago

cca_auth v0.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

cca_auth

šŸ” A robust Clean Architecture Authentication module for Node.js applications, providing enterprise-grade authentication, user management, and role-based access control.

npm version License: MIT

✨ Key Features

  • šŸ—ļø Clean Architecture Design: Follows best practices for maintainable and scalable code
  • šŸ” JWT Authentication: Secure token-based auth with access and refresh tokens
  • šŸ‘„ Role-Based Access Control: Built-in roles (ADMIN, USER, GUEST)
  • āœ… Input Validation: Robust request validation using Yup
  • šŸ—‘ļø Soft Delete: Safe data handling with soft delete functionality
  • šŸ“ TypeScript Support: Full TypeScript support with type definitions
  • šŸ”„ Error Handling: Consistent and informative error responses
  • šŸ“¦ Redis Support: Optional Redis integration for enhanced performance
  • šŸ”’ Security First: Built-in security features including password hashing and JWT protection

šŸ“¦ Installation

# Using npm
npm install cca_auth

# Using yarn
yarn add cca_auth

šŸš€ Quick Start

  1. Basic Setup
import { bootstrap, IServerConfig } from "cca_auth";

const config: IServerConfig = {
  port: 3000,
  apiPrefix: "/api/v1",
  databaseConfig: {
    host: "localhost",
    port: 5432,
    username: "postgres",
    password: "password",
    database: "auth_db",
  },
};

bootstrap(config)
  .then(() => console.log("āœ… Server started successfully"))
  .catch(console.error);
  1. Advanced Configuration
// Redis configuration (optional)
const redisConfig = {
  redisOn: true,
  url: "redis://localhost:6379",
};

// JWT configuration (optional)
const jwtConfig = {
  accessTokenSecret: "your-access-token-secret",
  refreshTokenSecret: "your-refresh-token-secret",
  accessTokenExpiry: "15m",
  refreshTokenExpiry: "7d",
};

bootstrap(config, redisConfig, jwtConfig);

āš™ļø Configuration Guide

Environment Variables

Create a .env file in your project root:

# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=your_secure_password
DB_NAME=auth_db
DB_LOGGING=true

# Application Configuration
NODE_ENV=development
API_PREFIX=/api/v1
PORT=3000

# JWT Configuration
JWT_ACCESS_SECRET=your-access-token-secret
JWT_REFRESH_SECRET=your-refresh-token-secret
JWT_ACCESS_EXPIRY=15m
JWT_REFRESH_EXPIRY=7d

# Redis Configuration (Optional)
REDIS_URL=redis://localhost:6379
REDIS_ENABLED=true

Configuration Interfaces

interface IServerConfig {
  port?: number;
  apiPrefix?: string;
  databaseConfig?: DatabaseConfig;
}

interface DatabaseConfig {
  host?: string;
  port?: number;
  username?: string;
  password?: string;
  database?: string;
  logging?: boolean;
  synchronize?: boolean;
  entities?: string[];
  migrations?: string[];
}

interface IRedis {
  redisOn?: boolean;
  url?: string;
}

interface IJwtConfig {
  accessTokenSecret: string;
  refreshTokenSecret: string;
  accessTokenExpiry: string;
  refreshTokenExpiry: string;
}

šŸ”— API Reference

Authentication Endpoints

/**
 * Register a new user
 * POST ${apiPrefix}/register
 */
{
    "email": "user@example.com",
    "name": "User Name",
    "password": "password123"
}

/**
 * Login
 * POST ${apiPrefix}/login
 */
{
    "email": "user@example.com",
    "password": "password123"
}

/**
 * Refresh Token
 * POST ${apiPrefix}/refresh
 */
{
    "refreshToken": "your-refresh-token"
}

User Management Endpoints

All endpoints require JWT authentication via Bearer token in the Authorization header.

// Get all users
GET ${apiPrefix}/users

// Get user by ID
GET ${apiPrefix}/users/:id

// Update user
PUT ${apiPrefix}/users/:id
{
    "name": "Updated Name",
    "email": "updated@example.com"
}

// Delete user (soft delete)
DELETE ${apiPrefix}/users/:id

šŸ”’ Security Features

  • Password Security: Automatic password hashing using bcrypt
  • JWT Protection: Secure token-based authentication
  • Role-Based Security: Fine-grained access control
  • Data Protection: Soft delete functionality for safe data handling
  • Input Validation: Request validation using Yup
  • Database Security: Secure TypeORM operations

šŸ› ļø Error Handling

The module provides consistent error responses across all endpoints:

{
    "status": "error",
    "message": "Detailed error message",
    "statusCode": 404  // HTTP status code
    "error": {         // Optional detailed error information
        "field": "email",
        "type": "validation"
    }
}

šŸ“š Dependencies

  • Core: Express.js, TypeScript
  • Database: TypeORM, PostgreSQL
  • Caching: Redis (optional)
  • Security: jsonwebtoken, bcrypt
  • Validation: Yup
  • Types: @types/node, @types/express

šŸ¤ Contributing

We welcome contributions! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

šŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

šŸ™‹ā€ā™‚ļø Support

  • šŸ“§ Email: mindaugaskul@gmail.com
0.1.10

10 months ago

0.1.11

10 months ago

0.1.12

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.9

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago