Home Projects Portfolio Dashboard Export PDF Log in

Acelerador 2.0: Securely Empowering Participants with Account Insights

Project Context

In the Acelerador 2.0 project, our goal is to provide a robust platform for participants. A key aspect of this involves enabling users to easily access their personal information related to program editions, participation status, and earned diplomas. This capability is crucial for enhancing user experience and transparency, allowing participants to keep track of their progress and achievements in real-time.

The Challenge: Exposing Sensitive User Data Securely

The primary challenge was to expose sensitive, user-specific data—such as their active program edition, their current enrollment status, and a list of their digital diplomas—through a set of secure, reliable, and easily consumable API endpoints. This required careful consideration of several factors:

  1. Authentication and Authorization: Ensuring that only authenticated and authorized users could access their own data.
  2. Data Consistency: Providing current and accurate information, often involving data aggregation from multiple sources.
  3. Structured Output: Presenting the data in a clear, consistent, and frontend-friendly format using Data Transfer Objects (DTOs).
  4. Error Handling: Gracefully managing scenarios where a user might not have specific data (e.g., no diplomas yet) without throwing errors.

The Solution: Implementing Secure Participant Endpoints

To address these challenges, we implemented a set of three new GET endpoints, all protected using JSON Web Tokens (JWT). These endpoints provide participants with a comprehensive overview of their involvement in Acelerador 2.0:

  • /edition/current: This endpoint delivers information about the currently active program edition or the one with an open call for applications. It also provides the edition status translated into Spanish for direct consumption by the frontend.
  • /edition-participant/my-status: Designed to give participants an immediate understanding of their current engagement, this endpoint returns their participation status within the active edition.
  • /certificate/mine: This endpoint provides a chronological list of all diplomas issued to the user, ordered by emission date. Crucially, it returns an empty array if no diplomas are found, ensuring a smooth user experience without triggering errors.

Each endpoint utilizes dedicated output DTOs to guarantee data consistency and simplify frontend integration. Authentication is handled by NestJS's AuthGuard configured for JWT, ensuring that all requests carry a valid token corresponding to an authenticated user.

Here’s a simplified example of a controller method demonstrating JWT protection and DTO usage:

import { Controller, Get, UseGuards, Req } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ParticipantService } from './participant.service';
import { ParticipantStatusDto } from './dto/participant-status.dto';

@ApiTags('Participant')
@ApiBearerAuth()
@Controller('edition-participant')
export class ParticipantController {
  constructor(private readonly participantService: ParticipantService) {}

  @UseGuards(AuthGuard('jwt'))
  @Get('my-status')
  @ApiOperation({ summary: 'Get current participation status' })
  @ApiResponse({ status: 200, type: ParticipantStatusDto, description: 'Current participation status of the user' })
  async getMyStatus(@Req() req): Promise<ParticipantStatusDto> {
    const userId = req.user.id; // User ID extracted from JWT payload
    return this.participantService.findCurrentParticipantStatus(userId);
  }
}

This ParticipantController method, getMyStatus, uses @UseGuards(AuthGuard('jwt')) to ensure the incoming request has a valid JWT. It then extracts the user ID from the token and calls a service method to retrieve the relevant status, returning it as a ParticipantStatusDto.

Practical Implementation and Benefits

The implementation leveraged NestJS for structuring the API, ensuring a modular and scalable approach. TypeORM was instrumental in managing database interactions, with entities correctly registered using TypeOrmModule.forFeature in their respective modules. Swagger annotations were added to all new endpoints and DTOs, providing clear and up-to-date API documentation.

The benefits of this approach are significant:

  • Enhanced User Experience: Participants gain immediate and secure access to critical information, improving their engagement with the platform.
  • Robust Security: JWT protection ensures that sensitive data is only accessible to legitimate users.
  • Developer Efficiency: Standardized DTOs and API design simplify frontend development and integration.
  • Maintainability: A clear separation of concerns (controller, service, repository, DTOs) makes the codebase easier to understand and maintain.

Key Takeaways for API Development

When building user-centric APIs, especially those handling personal data, prioritize:

  1. Security First: Implement robust authentication and authorization mechanisms from the outset.
  2. Clear Contracts: Define explicit DTOs for all API responses to ensure predictable data structures.
  3. User Experience in Mind: Design endpoints that are intuitive for the frontend to consume and handle edge cases gracefully (e.g., empty lists instead of errors).
  4. Documentation: Use tools like Swagger to keep your API documentation current and accessible.

Architectural Insight

An API-first approach, coupled with strong architectural patterns like Dependency Injection and the Repository Pattern (implicitly used with TypeORM), fosters highly maintainable and scalable applications. By defining clear interfaces for data access and business logic, we create a system that is resilient to change and easy to extend, ultimately leading to a more stable and feature-rich user experience.


Generated with Gitvlg.com

Daniela Homobono

Daniela Homobono

Author

Share: