Skip to main content

Authentication Module Documentation - EML Backend

1. Introduction

The Authentication module in EML Backend manages user authentication, credential validation, and access token issuance to ensure platform security. It implements JWT for token-based authentication and OAuth2 for integration with external services.


2. Code Structure

modules/auth/
├── auth.module.ts # Module definition
├── auth.controller.ts # Handles HTTP requests
├── auth.service.ts # Authentication logic
├── auth.guard.ts # Middleware for route protection
├── strategies/ # Authentication strategies (JWT, OAuth)
│ ├── jwt.strategy.ts
│ ├── local.strategy.ts
├── dto/ # Data Transfer Object definitions
│ ├── login.dto.ts
│ ├── register.dto.ts

3. Available Endpoints

3.1 User Authentication

POST /auth/login - Logs in and retrieves a JWT token.

Request Example:

curl -X POST http://localhost:3000/auth/login      -H "Content-Type: application/json"      -d '{"email": "[email protected]", "password": "123456"}'

Response Example:

{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5c...",
"expiresIn": 3600
}

3.2 User Registration

POST /auth/register - Creates a new user account.

Request Example:

curl -X POST http://localhost:3000/auth/register      -H "Content-Type: application/json"      -d '{"name": "Juan Perez", "email": "[email protected]", "password": "123456"}'

Response Example:

{
"id": 1,
"name": "Juan Perez",
"email": "[email protected]"
}

3.3 Token Validation

GET /auth/profile - Retrieves data of the authenticated user.

Request Example:

curl -X GET http://localhost:3000/auth/profile      -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."

Response Example:

{
"id": 1,
"name": "Juan Perez",
"email": "[email protected]"
}

4. Security and Middleware

  • AuthGuard: Middleware that protects restricted routes.
  • JWT Expiration: Tokens are valid for 1 hour, with an optional refresh.
  • Available Strategies:
    • JWT Strategy: Token-based authentication.
    • Local Strategy: Authentication using username/password.

Example of AuthGuard implementation in a controller:

@Get('profile')
@UseGuards(AuthGuard('jwt'))
async getProfile(@Request() req) {
return req.user;
}

5. Dependencies with Other Modules

  • Users: Connects with the user module for credential validation.
  • Notifications: Can send a welcome email after user registration.

6. Additional Notes

  • To enhance security, enabling 2FA (Two-Factor Authentication) is recommended in future versions.
  • Future implementation of OAuth2 with Google and Facebook for external login.

7. Conclusion

The Authentication module is essential for secure access to EML Backend. It ensures data protection and user validation through JWT and secure strategies. For more details, review the configuration in auth.module.ts.

🚀 This document will be updated as the system evolves.