Jelajahi Sumber

Bugfix: Improve registration error handling and response messages; add utility for formatting validation errors

Co-authored-by: Copilot <copilot@github.com>
emrecevik106 1 bulan lalu
induk
melakukan
ff13902707

+ 7 - 16
src/actions/auth/register/index.ts

@@ -10,25 +10,18 @@ import {
 import {
     RegisterInput, RegisterResult 
 } from "./types";
-
-/* const isStrongPassword = (password: string): { valid: boolean; message: string } => {
-    if (password.length < 8) return { message: "Password must be at least 8 characters", valid: false };
-    if (!/[A-Z]/.test(password)) return { message: "Password must contain at least 1 uppercase letter", valid: false };
-    if (!/[a-z]/.test(password)) return { message: "Password must contain at least 1 lowercase letter", valid: false };
-    if (!/[0-9]/.test(password)) return { message: "Password must contain at least 1 number", valid: false };
-    if (!/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) return { message: "Password must contain at least 1 special character", valid: false };
-    return { message: "", valid: true };
-}; */
+import {
+    formatValidationErrors 
+} from "../../../utils";
 
 export const register = async (input: RegisterInput): Promise<RegisterResult> => {
     const dto = plainToInstance(RegisterInput, input);
     const errors = await validate(dto);
 
     if (errors.length > 0) {
-        const message = Object.values(errors[0].constraints!)[0];
         return {
+            message: formatValidationErrors(errors),
             code: 400,
-            message, 
         };
     }
 
@@ -44,10 +37,9 @@ export const register = async (input: RegisterInput): Promise<RegisterResult> =>
     const existingUser = await User.findOne({
         mail 
     });
-
     if (existingUser) {
         return {
-            message: "Email already in use", 
+            message: "email-already-in-use", 
             code: 409 
         };
     }
@@ -55,10 +47,9 @@ export const register = async (input: RegisterInput): Promise<RegisterResult> =>
     const existingPhone = await User.findOne({
         phoneNumber 
     });
-    
     if (existingPhone) {
         return {
-            message: "Phone number already in use", 
+            message: "phone-already-in-use", 
             code: 409 
         };
     }
@@ -74,7 +65,7 @@ export const register = async (input: RegisterInput): Promise<RegisterResult> =>
     });
 
     return {
-        message: "Registration successful",
+        message: "registration-successful",
         code: 201 
     };
 };

+ 1 - 1
src/actions/auth/register/types.ts

@@ -30,6 +30,6 @@ export class RegisterInput {
 }
 
 export interface RegisterResult {
-    message: string;
+    message: string | string[];
     code: number;
 }

+ 1 - 1
src/middlewares/authMiddleware.ts

@@ -4,7 +4,7 @@ import {
 import jwt from "jsonwebtoken";
 
 export interface AuthRequest extends Request {
-  userId?: string;
+    userId?: string;
 }
 
 export const authMiddleware = (req: AuthRequest, res: Response, next: NextFunction): void => {

+ 7 - 0
src/utils/index.ts

@@ -0,0 +1,7 @@
+import {
+    ValidationError 
+} from "class-validator";
+
+export const formatValidationErrors = (errors: ValidationError[]): string[] => {
+    return errors.map(error => Object.values(error.constraints!)[0]);
+};