cca_auth v0.1.12
cca_auth
š A robust Clean Architecture Authentication module for Node.js applications, providing enterprise-grade authentication, user management, and role-based access control.
⨠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
- 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);
- 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:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
š License
This project is licensed under the MIT License - see the LICENSE file for details.
šāāļø Support
- š§ Email: mindaugaskul@gmail.com
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago