Enhance authentication and user service error handling
- Added HttpCode decorator to the checkPasswordNeeded endpoint for consistent response status. - Updated email validation in UsersService to throw BadRequestException for improved error handling. - Introduced NotFoundException in WhmcsConnectionService for better error messaging when clients are not found.
This commit is contained in:
parent
3f01656bbb
commit
79f11edb65
@ -1,4 +1,4 @@
|
||||
import { Controller, Post, Body, UseGuards, Get, Req } from "@nestjs/common";
|
||||
import { Controller, Post, Body, UseGuards, Get, Req, HttpCode } from "@nestjs/common";
|
||||
import type { Request } from "express";
|
||||
import { Throttle } from "@nestjs/throttler";
|
||||
import { AuthService } from "./auth.service";
|
||||
@ -116,6 +116,7 @@ export class AuthController {
|
||||
|
||||
@Public()
|
||||
@Post("check-password-needed")
|
||||
@HttpCode(200)
|
||||
@ApiOperation({ summary: "Check if user needs to set password" })
|
||||
@ApiResponse({ status: 200, description: "Password status checked" })
|
||||
async checkPasswordNeeded(@Body() { email }: { email: string }) {
|
||||
|
||||
@ -121,8 +121,10 @@ export class UsersService {
|
||||
|
||||
private validateEmail(email: string): string {
|
||||
const trimmed = email?.toLowerCase().trim();
|
||||
if (!trimmed) throw new Error("Email is required");
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) throw new Error("Invalid email format");
|
||||
if (!trimmed) throw new BadRequestException("Email is required");
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) {
|
||||
throw new BadRequestException("Invalid email format");
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { getErrorMessage } from "../../../common/utils/error.util";
|
||||
import { Logger } from "nestjs-pino";
|
||||
import { Injectable, Inject } from "@nestjs/common";
|
||||
import { Injectable, Inject, NotFoundException } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import {
|
||||
WhmcsApiResponse,
|
||||
@ -171,6 +171,21 @@ export class WhmcsConnectionService {
|
||||
params: this.sanitizeLogParams(params),
|
||||
authModeTried: useAdminAuth ? "admin" : "api_credentials",
|
||||
});
|
||||
// Normalize common, expected error responses to domain exceptions
|
||||
if (
|
||||
action === "GetClientsDetails" &&
|
||||
typeof errorResponse.message === "string" &&
|
||||
errorResponse.message.toLowerCase().includes("client not found")
|
||||
) {
|
||||
const byEmail = typeof (params as any).email === "string" ? (params as any).email : undefined;
|
||||
if (byEmail) {
|
||||
throw new NotFoundException(`Client with email ${byEmail} not found`);
|
||||
}
|
||||
const byId = (params as any).clientid;
|
||||
throw new NotFoundException(
|
||||
`Client ${typeof byId === "string" || typeof byId === "number" ? byId : ""} not found`
|
||||
);
|
||||
}
|
||||
throw new Error(`WHMCS API Error: ${errorResponse.message}`);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user