feat: add logging and caching enhancements across core services
Integrate `Logger` for consistent logging in services like `reports`, `categories`, `users`, `contents`, and more. Introduce caching capabilities with `CacheInterceptor` and manual cache clearing logic for categories, users, and contents. Add request throttling to critical auth endpoints for enhanced rate limiting.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Body, Controller, Headers, Post, Req, Res } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Throttle } from "@nestjs/throttler";
|
||||
import type { Request, Response } from "express";
|
||||
import { getIronSession } from "iron-session";
|
||||
import { AuthService } from "./auth.service";
|
||||
@@ -16,11 +17,13 @@ export class AuthController {
|
||||
) {}
|
||||
|
||||
@Post("register")
|
||||
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
||||
register(@Body() registerDto: RegisterDto) {
|
||||
return this.authService.register(registerDto);
|
||||
}
|
||||
|
||||
@Post("login")
|
||||
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
||||
async login(
|
||||
@Body() loginDto: LoginDto,
|
||||
@Headers("user-agent") userAgent: string,
|
||||
@@ -52,6 +55,7 @@ export class AuthController {
|
||||
}
|
||||
|
||||
@Post("verify-2fa")
|
||||
@Throttle({ default: { limit: 5, ttl: 60000 } })
|
||||
async verifyTwoFactor(
|
||||
@Body() verify2faDto: Verify2faDto,
|
||||
@Headers("user-agent") userAgent: string,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
Logger,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
@@ -14,6 +15,8 @@ import { RegisterDto } from "./dto/register.dto";
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private readonly logger = new Logger(AuthService.name);
|
||||
|
||||
constructor(
|
||||
private readonly usersService: UsersService,
|
||||
private readonly cryptoService: CryptoService,
|
||||
@@ -22,6 +25,7 @@ export class AuthService {
|
||||
) {}
|
||||
|
||||
async generateTwoFactorSecret(userId: string) {
|
||||
this.logger.log(`Generating 2FA secret for user ${userId}`);
|
||||
const user = await this.usersService.findOne(userId);
|
||||
if (!user) throw new UnauthorizedException();
|
||||
|
||||
@@ -42,6 +46,7 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async enableTwoFactor(userId: string, token: string) {
|
||||
this.logger.log(`Enabling 2FA for user ${userId}`);
|
||||
const secret = await this.usersService.getTwoFactorSecret(userId);
|
||||
if (!secret) {
|
||||
throw new BadRequestException("2FA not initiated");
|
||||
@@ -57,6 +62,7 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async disableTwoFactor(userId: string, token: string) {
|
||||
this.logger.log(`Disabling 2FA for user ${userId}`);
|
||||
const secret = await this.usersService.getTwoFactorSecret(userId);
|
||||
if (!secret) {
|
||||
throw new BadRequestException("2FA not enabled");
|
||||
@@ -72,6 +78,7 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async register(dto: RegisterDto) {
|
||||
this.logger.log(`Registering new user: ${dto.username}`);
|
||||
const { username, email, password } = dto;
|
||||
|
||||
const passwordHash = await this.cryptoService.hashPassword(password);
|
||||
@@ -91,6 +98,7 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async login(dto: LoginDto, userAgent?: string, ip?: string) {
|
||||
this.logger.log(`Login attempt for email: ${dto.email}`);
|
||||
const { email, password } = dto;
|
||||
|
||||
const emailHash = await this.cryptoService.hashEmail(email);
|
||||
@@ -141,6 +149,7 @@ export class AuthService {
|
||||
userAgent?: string,
|
||||
ip?: string,
|
||||
) {
|
||||
this.logger.log(`2FA verification attempt for user ${userId}`);
|
||||
const user = await this.usersService.findOneWithPrivateData(userId);
|
||||
if (!user || !user.isTwoFactorEnabled) {
|
||||
throw new UnauthorizedException();
|
||||
|
||||
Reference in New Issue
Block a user