Skip to content

Utilities API

The Utilities API provides a set of common functionalities that can be used across different applications. These utilities are designed to simplify the development process and improve the overall user experience.

Decorator

@RequireAuth()

This decorator is used to protect routes that require authentication. It checks if the user is authenticated before allowing access to the route. If the user is not authenticated, it will return a 401 Unauthorized error.

Example

Implement authentication for all routes in the UserController:

ts
import { RequireAuth } from 'src/decorators/require-auth.decorator';

@RequireAuth()
@Controller('user')
export class UserController {
  constructor(
    private readonly profileService: ProfileService
  ) { }

  @RequireAuth() // or only for specific routes
  @Get('me')
  me(@Req() req: Request) {
    return this.profileService.getProfile(req.user.id)
  }
}

@UseGuards(RequirePasswordGuard)

This guard is used to protect routes that require a password. It checks if the user has entered the correct password before allowing access to the route. If the password is incorrect, it will return a 401 Unauthorized error.

Example

Implement password protection for the updatePassword route in the UserController:

ts
import { RequirePasswordGuard } from 'src/guard/require-password';
@RequireAuth()
@Controller('user')
export class UserController {
  @UseGuards(RequirePasswordGuard)
  @Patch('password')
  async updatePassword(@Body() body: ProfilePasswordDto, @Req() req: Request) {
    return this.profileService.updatePassword(req.user.id, body.newPassword)
  }
}