Parcourir la source

Feature: Enhance registration and SMS OTP functionality with validation and error handling

emrecevik106 il y a 1 mois
Parent
commit
d52ba03ce7

+ 62 - 38
src/actions/auth/register/index.ts

@@ -1,6 +1,7 @@
 import {
     User
 } from "../../../models/User";
+import smsSend from "../smsSend";
 import {
     RegisterInput,
     RegisterResult
@@ -22,50 +23,73 @@ const generateSlug = (companyName: string): string => {
 };
 
 const register = async (input: RegisterInput): Promise<RegisterResult> => {
-    const {
-        companyName,
-        phoneNumber,
-        firstName,
-        lastName,
-        password,
-        mail
-    } = input;
+    try {
+        const {
+            companyName,
+            phoneNumber,
+            firstName,
+            lastName,
+            password,
+            mail
+        } = input;
+
+        const existingUser = await User.findOne({
+            mail
+        });
+        if (existingUser) {
+            return {
+                message: "email-already-in-use",
+                code: 409
+            };
+        }
+
+        const existingPhone = await User.findOne({
+            phoneNumber
+        });
+        if (existingPhone) {
+            return {
+                message: "phone-already-in-use",
+                code: 409
+            };
+        }
+
+        const slug = generateSlug(companyName);
+        const existingSlug = await User.findOne({
+            slug 
+        });
+
+        if (existingSlug) {
+            return {
+                message: "company-name-already-in-use",
+                code: 409
+            };
+        }
+
+        const newUser = await User.create({
+            fullName: `${firstName} ${lastName}`,
+            phoneNumber,
+            companyName,
+            firstName,
+            lastName,
+            password,
+            slug,
+            mail
+        });
+        await smsSend({
+            userID: newUser._id.toString() 
+        });
 
-    const existingUser = await User.findOne({
-        mail
-    });
-    if (existingUser) {
         return {
-            message: "email-already-in-use",
-            code: 409
+            message: "registration-successful-please-verify-your-phone",
+            code: 201
         };
-    }
-
-    const existingPhone = await User.findOne({
-        phoneNumber
-    });
-    if (existingPhone) {
+    } catch (error) {
+        console.error("FinishMailVerify error:", error);
         return {
-            message: "phone-already-in-use",
-            code: 409
+            message: "internal-server-error",
+            code: 500
         };
     }
-
-    await User.create({
-        fullName: `${firstName} ${lastName}`,
-        slug: generateSlug(companyName),
-        phoneNumber,
-        companyName,
-        firstName,
-        lastName,
-        password,
-        mail
-    });
-
-    return {
-        message: "registration-successful",
-        code: 201
-    };
 };
 
 export default register;

+ 10 - 2
src/actions/auth/smsOTP/index.ts

@@ -1,3 +1,4 @@
+import mongoose from "mongoose";
 import {
     User
 } from "../../../models/User";
@@ -14,6 +15,13 @@ const smsOTP = async (input: SmsOTPInput): Promise<SmsOTPResult> => {
             code
         } = input;
 
+        if (!mongoose.Types.ObjectId.isValid(userID)) {
+            return {
+                message: "user-not-found",
+                code: 404
+            };
+        }
+        
         const user = await User.findById(userID);
         if (!user) {
             return {
@@ -36,17 +44,17 @@ const smsOTP = async (input: SmsOTPInput): Promise<SmsOTPResult> => {
                 code: 400
             };
         }
-
+        
         await User.findByIdAndUpdate(userID, {
             isPhoneVerified: true
         });
-
         await redis.del(`sms-verify-${userID}`);
 
         return {
             message: "your-request-has-been-received-we-will-contact-you-shortly",
             code: 200
         };
+        
     } catch (error) {
         console.error("smsOTP error:", error);
         return {

+ 2 - 2
src/actions/auth/smsOTP/types.ts

@@ -6,11 +6,11 @@ import {
 export class SmsOTPInput {
     @IsString()
     @IsNotEmpty({ message: "userID-is-required" })
-    userID?: string;
+    userID!: string;
 
     @IsString()
     @IsNotEmpty({ message: "code-is-required" })
-    code?: string;
+    code!: string;
 }
 
 export interface SmsOTPResult {

+ 5 - 2
src/routes/authRoutes.ts

@@ -8,6 +8,7 @@ import {
     register,
     smsSend,
     logout,
+    smsOTP,
     login,
     me
 } from "../controllers/authController";
@@ -24,19 +25,21 @@ import {
     RefreshTokenInput,
     RegisterInput,
     SmsSendInput,
+    SmsOTPInput,
     LoginInput
 } from "../actions/auth/types/index";
 
 const router = Router();
 
-router.post("/finishMailVerify", validateBody(FinishMailVerifyInput), finishMailVerify);
-router.post("/startMailVerify", validateBody(StartMailVerifyInput), startMailVerify);
+router.post("/finishMailVerify", validateBody(FinishMailVerifyInput), finishMailVerify); //TODO: auth middleware eklenecek
+router.post("/startMailVerify", validateBody(StartMailVerifyInput), startMailVerify); //TODO: auth middleware eklenecek
 router.post("/refreshToken", validateBody(RefreshTokenInput), refreshToken);
 router.post("/register", validateBody(RegisterInput), register);
 router.post("/login", validateBody(LoginInput), login);
 router.post("/logout", authMiddleware, logout);
 
 router.post("/smsSend", validateBody(SmsSendInput), smsSend);
+router.post("/smsOTP", validateBody(SmsOTPInput), smsOTP);
 
 router.get("/validateToken", authMiddleware, (req: AuthRequest, res) => {
     res.status(200)