How rachidchabane/userservice works
Main Components
• External Clients: These could be the frontend applications that send HTTP requests that contain JWT tokens.
• Auth0 (External Identity Provider): Represents the authentication service used for user login, registration, and JWT issuance.
• Web/API Server (Node.js + Express): This is the core of the microservice and includes several sub-components:
• Routes: Incoming requests are routed (see src/routes) to the appropriate endpoint.
• Controllers: Business logic handlers (such as in src/controllers/user.controller.ts) that process requests.
• Middleware: Includes JWT validation, user sync, error handing, and input validation (found in src/middleware).
• Services: Encapsulate business logic and external communications (e.g., src/services/auth0.service.ts, src/services/user.service.ts) and interact with the database.
• Repositories: Direct data access to MongoDB (as seen in src/repositories/user.repository.ts).
• Models and Validations: Define data schema (src/models) and data validation rules (src/validations).
Data Flow and Interactions
• Client sends an HTTP request (with a JWT token when needed) to an exposed endpoint (the API routes).
• The request first passes through middleware layers:
• validateJwt middleware verifies the token.
• If it’s a secured route, the syncUser middleware retrieves or creates the user record in MongoDB.
• Validation middleware checks the payload (for example, when updating the user profile).
• The request is then passed to Controllers which invoke services:
• The user controller further calls the business service (user.service) that encapsulates logic such as getCurrentUser or updateProfile.
• The service interacts with the repository layer for MongoDB operations.
• In parallel, when authentication is needed, the Auth0 service interacts with the external Auth0 platform, validating tokens and managing user identity.
• Finally, the response is sent back to the client.
Architectural Patterns or Design Principles
• Layered Architecture: Separation between routes, controllers, middleware, services, and repositories.
• Separation of Concerns: Dedicated responsibilities for authentication (via Auth0) and authorization/data management (via local MongoDB).
• Statelessness: The use of JWT tokens and no server-side session state facilitates horizontal scaling.
• Testability: The project includes clear directories for tests (__tests__) corresponding to controllers, middleware, and services.