Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nieladverse/users_api

Users
https://github.com/nieladverse/users_api

javascript jwt-authentication mongodb nestjs typescript

Last synced: 3 days ago
JSON representation

Users

Awesome Lists containing this project

README

        

# User Management and Authentication Microservices

A robust backend implementation showcasing microservices architecture with NestJS, featuring comprehensive user management and JWT-based authentication services. This API is publicly available for developers to integrate into their applications.

## ๐ŸŒ Public API Access

This API is publicly available for developers! Feel free to use it in your projects.

### Base URL
```
https://users-5x41.onrender.com/
```

### Rate Limiting
- 100 requests per hour per IP address
- Additional rate limits may apply to specific endpoints

### Authentication
To use protected endpoints:
1. Create an account using the registration endpoint
2. Obtain your JWT token through the login endpoint
3. Include the token in your requests' Authorization header:
```
Authorization: Bearer your_jwt_token
```

## ๐Ÿš€ Features

### User Service
- Complete CRUD operations for user management
- Secure password handling with bcrypt
- Email-based user lookup
- Protected routes with JWT authentication
- MongoDB integration for persistent storage

### Authentication Service
- JWT-based authentication
- Secure login/logout functionality
- Token renewal system
- Refresh token mechanism
- Request validation using pipes

## ๐Ÿ“ก API Usage Examples

### Creating a New User
```bash
curl -X POST https://users-5x41.onrender.com/users \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "securepassword",
"username": "newuser"
}'
```

### Login
```bash
curl -X POST https://users-5x41.onrender.com/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "securepassword"
}'
```

### Getting User Details (Protected Route)
```bash
curl -X GET https://users-5x41.onrender.com/users/me \
-H "Authorization: Bearer your_jwt_token"
```

## ๐Ÿ› ๏ธ Technical Stack

- **Framework:** NestJS
- **Database:** MongoDB
- **Authentication:** JSON Web Tokens (JWT)
- **Password Security:** bcrypt

## ๐Ÿ—๏ธ Architecture & Design Patterns

- **Dependency Injection:** Utilizing NestJS's powerful DI container
- **Repository Pattern:** MongoDB models serving as data access layer
- **DTO Pattern:** Data Transfer Objects for request validation
- **Guard Pattern:** JWT authentication guards for route protection
- **Exception Filters:** Centralized error handling
- **Async/Await:** Modern asynchronous programming patterns

## ๐Ÿ“‹ API Endpoints

### Public Endpoints

```markdown
POST /auth/login - User login
POST /users - Create new user
```

### Protected Endpoints (Require Authentication)

```markdown
GET /users - Retrieve all users
GET /users/:id - Retrieve specific user
DELETE /users/:id - Delete user
PUT /users/:id - Update user
GET /users/email/:email - Find user by email
POST /auth/logout - User logout
POST /auth/renew-token - Refresh access token
```

## ๐Ÿ“Š Response Formats

All API responses follow this standard format:

```json
{
"success": true,
"data": {
// Response data here
},
"message": "Operation successful"
}
```

### Error Response Format
```json
{
"success": false,
"error": {
"code": "ERROR_CODE",
"message": "Error description"
}
}
```

## ๐Ÿ”’ Security Features

- JWT-based authentication
- Password hashing with bcrypt
- Protected routes using guards
- Request validation
- Token refresh mechanism
- Secure logout handling

## ๐Ÿ“š API Documentation

Interactive API documentation is available at:
```
https://users-5x41.onrender.com/
```

Features:
- Interactive endpoint testing
- Request/response examples
- Authentication guide
- Schema definitions

## ๐Ÿ’ฌ Support

Need help? Here's how you can reach us:
- Create an issue in this repository
- Email: [[email protected]]

## ๐Ÿ“ License

This project is licensed under the MIT License - feel free to use it in your own projects!

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

## โญ Show your support

Give a โญ๏ธ if this project helped you!

## ๐Ÿงช Testing

This project implements comprehensive testing using Jest, ensuring reliability and maintainability across all services.

### Test Coverage

```bash
--------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|-------------------
All files | 97.32 | 95.18 | 96.47 | 97.12 |
src/auth | 98.21 | 96.43 | 97.14 | 98.11 |
auth.service.ts | 98.55 | 96.77 | 97.50 | 98.48 | 145-146
auth.controller.ts | 97.87 | 96.15 | 96.77 | 97.73 | 89
src/users | 96.43 | 93.94 | 95.83 | 96.15 |
users.service.ts | 96.77 | 94.12 | 95.45 | 96.55 | 178,212
users.controller.ts | 96.08 | 93.75 | 96.15 | 95.74 | 67,145
--------------------------|---------|----------|---------|---------|-------------------
```

### Test Types

- **Unit Tests**: Individual components testing
```typescript
describe('UsersService', () => {
it('should create a new user', async () => {
// Test implementation
});
});
```

- **Integration Tests**: Service interactions testing
```typescript
describe('AuthService', () => {
it('should authenticate user and return JWT', async () => {
// Test implementation
});
});
```

- **E2E Tests**: Full API endpoint testing
```typescript
describe('Users endpoints', () => {
it('GET /users should return all users', async () => {
// Test implementation
});
});
```

### Test Scripts

```bash
# Run all tests
npm run test

# Run tests with coverage
npm run test:cov

# Run tests in watch mode
npm run test:watch

# Run e2e tests
npm run test:e2e
```

### Key Testing Features

- **Mocking**: Extensive use of Jest mocks for external dependencies
- **Fixtures**: Pre-defined test data for consistent testing
- **Custom Matchers**: Special assertions for JWT and password handling
- **Error Cases**: Comprehensive testing of error scenarios
- **Guard Testing**: Authentication and authorization tests

### Test Directory Structure

```
tests/
โ”œโ”€โ”€ unit/
โ”‚ โ”œโ”€โ”€ auth/
โ”‚ โ”‚ โ”œโ”€โ”€ auth.service.spec.ts
โ”‚ โ”‚ โ””โ”€โ”€ auth.controller.spec.ts
โ”‚ โ””โ”€โ”€ users/
โ”‚ โ”œโ”€โ”€ users.service.spec.ts
โ”‚ โ””โ”€โ”€ users.controller.spec.ts
โ”œโ”€โ”€ integration/
โ”‚ โ”œโ”€โ”€ auth.integration.spec.ts
โ”‚ โ””โ”€โ”€ users.integration.spec.ts
โ””โ”€โ”€ e2e/
โ”œโ”€โ”€ auth.e2e-spec.ts
โ””โ”€โ”€ users.e2e-spec.ts
```

### Example Test Case

```typescript
describe('UsersService', () => {
let service: UsersService;
let model: Model;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{
provide: getModelToken(User.name),
useValue: {
new: jest.fn().mockResolvedValue(mockUser),
constructor: jest.fn().mockResolvedValue(mockUser),
find: jest.fn(),
create: jest.fn(),
exec: jest.fn(),
},
},
],
}).compile();

service = module.get(UsersService);
model = module.get>(getModelToken(User.name));
});

it('should create a new user successfully', async () => {
const createUserDto = {
email: '[email protected]',
password: 'password123',
username: 'testuser',
};

jest.spyOn(model, 'create').mockImplementationOnce(() =>
Promise.resolve({
id: 'some-id',
...createUserDto,
password: 'hashed_password',
} as any),
);

const result = await service.createUser(createUserDto);
expect(result).toBeDefined();
expect(result.email).toBe(createUserDto.email);
});
});
```

### Best Practices Implemented

- **Isolated Tests**: Each test runs in isolation with its own data
- **Clean Setup/Teardown**: Proper test environment management
- **Meaningful Assertions**: Clear test expectations
- **Error Testing**: Validation of error handling
- **Mock Data**: Standardized test fixtures

### Running Tests Locally

```bash
# Install dependencies
npm install

# Run all tests with coverage
npm run test:cov

# Run specific test file
npm run test -- users.service.spec.ts

# Run tests in watch mode
npm run test:watch
```