فهرست منبع

Merge branch 'bugfix/startMailVerified-bugfix' into develop

BedirhanOZCAN 1 ماه پیش
والد
کامیت
ccd8556aca

+ 1 - 1
src/actions/auth/finishMailVerified/index.ts

@@ -10,7 +10,7 @@ import {
 const finishMailVerify = async (input: FinishMailVerifyInput): Promise<FinishMailVerifyResult> => {
     try {
         const {
-            userID,
+            userID, // TODO userID kaldırılıp mail kullanılacak. maile göre user bulunacak.
             code
         } = input;
 

+ 8 - 2
src/actions/auth/index.ts

@@ -1,3 +1,6 @@
+import {
+    finishMailVerify
+} from "./../../controllers/authController";
 export {
     default as login
 } from "./login";
@@ -11,8 +14,11 @@ export {
     default as me
 } from "./me";
 export {
-    default as refreshToken 
+    default as refreshToken
 } from "./refreshToken";
 export {
-    default as finishMailVerify 
+    default as finishMailVerify
 } from "./finishMailVerified";
+export {
+    default as startMailVerify
+} from "./startMailVerify";

+ 5 - 2
src/actions/auth/logout/index.ts

@@ -17,7 +17,8 @@ export const logout = async (context?: ContextType): Promise<LogoutResult> => {
         }
 
         const {
-            userID, token
+            userID,
+            token
         } = context;
 
         if (!userID || !token) {
@@ -44,4 +45,6 @@ export const logout = async (context?: ContextType): Promise<LogoutResult> => {
             code: 500,
         };
     }
-};
+};
+
+export default logout;

+ 17 - 28
src/actions/auth/me/index.ts

@@ -2,19 +2,13 @@ import {
     User
 } from "../../../models/User";
 import {
-    ContextType, MeResult
+    ContextType,
+    MeResult
 } from "./types";
 
-const me = async (context?: ContextType): Promise<MeResult> => {
+const me = async (context: ContextType): Promise<MeResult> => {
     try {
-        if (!context || !context.userID) {
-            return {
-                message: "unauthorized",
-                code: 401,
-            };
-        }
-
-        const user = await User.findById(context.userID, {
+        const user = (await User.findById(context.userID, {
             phoneNumber: 1,
             companyName: 1,
             firstName: 1,
@@ -22,29 +16,24 @@ const me = async (context?: ContextType): Promise<MeResult> => {
             lastName: 1,
             mail: 1,
             _id: 1,
-        });
+        }))!;
 
-        if (!user) {
-            return {
-                message: "user-not-found",
-                code: 404,
-            };
-        }
+        const payload = {
+            user: {
+                phoneNumber: user.phoneNumber,
+                companyName: user.companyName,
+                userID: user._id.toString(),
+                firstName: user.firstName,
+                lastName: user.lastName,
+                fullName: user.fullName,
+                mail: user.mail
+            }
+        };
 
         return {
             message: "user-profile-retrieved",
             code: 200,
-            payload: {
-                user: {
-                    phoneNumber: user.phoneNumber,
-                    companyName: user.companyName,
-                    userID: user._id.toString(),
-                    firstName: user.firstName,
-                    lastName: user.lastName,
-                    fullName: user.fullName,
-                    mail: user.mail
-                }
-            }
+            payload
         };
     } catch (error) {
         console.error("Me action error:", error);

+ 5 - 4
src/actions/auth/startMailVerified/index.ts → src/actions/auth/startMailVerify/index.ts

@@ -6,12 +6,16 @@ import {
     StartMailVerifyResult,
     StartMailVerifyInput
 } from "./types";
+import {
+    verificationCode,
+    TTL_SECONDS
+} from "../../../utils";
 // import { sendMail } from "../../../utils/mailer"; // mail gönderme fonskiyonu bu şekilde ilerde import edilecek.
 
 const startMailVerify = async (input: StartMailVerifyInput): Promise<StartMailVerifyResult> => {
     try {
         const {
-            userID
+            userID // TODO MAİL ALINACAK DTO İLE YAPIALCAK MAİL İLE USER ARANCAK MONGO DA 
         } = input;
 
         if (!userID) {
@@ -47,9 +51,6 @@ const startMailVerify = async (input: StartMailVerifyInput): Promise<StartMailVe
                 }
             };
         }
-        //TODO BURADAKİ KOD ÜRETİMİ FARKLI BİR DOSYADAN MI ÜRETİLCEK UTİLS GİBİ
-        const verificationCode = Math.floor(100000 + Math.random() * 900000).toString();
-        const TTL_SECONDS = 180; // Kodun geçerlilik süresi bu.
 
         await redis.setex(`mail-verify-${userID}`, TTL_SECONDS, verificationCode);
 

+ 0 - 0
src/actions/auth/startMailVerified/types.ts → src/actions/auth/startMailVerify/types.ts


+ 32 - 3
src/controllers/authController.ts

@@ -3,8 +3,9 @@ import {
     Request
 } from "express";
 import {
-    refreshToken as _refreshToken,
     finishMailVerify as _finishMailVerify,
+    startMailVerify as _startMailVerify,
+    refreshToken as _refreshToken,
     register as _register,
     logout as _logout,
     login as _login,
@@ -99,7 +100,7 @@ export const logout = async (req: AuthRequest, res: Response): Promise<void> =>
 
 export const me = async (req: AuthRequest, res: Response): Promise<void> => {
     try {
-        const result = await _me(req.context);
+        const result = await _me(req.context!);
 
         res.status(result.code).json({
             message: result.message,
@@ -110,7 +111,8 @@ export const me = async (req: AuthRequest, res: Response): Promise<void> => {
         });
     } catch (error) {
         res.status(500).json({
-            message: "server-error", code: 500
+            message: "server-error",
+            code: 500
         });
     }
 };
@@ -141,6 +143,33 @@ export const refreshToken = async (req: Request, res: Response): Promise<void> =
     }
 };
 
+export const startMailVerify = async (req: Request, res: Response): Promise<void> => {
+    try {
+        const {
+            userID
+        } = req.body;
+
+        const result = await _startMailVerify({
+            userID
+        });
+
+        res.status(result.code).json({
+            message: result.message,
+            code: result.code,
+            ...(result.payload && {
+                payload: result.payload
+            }),
+        });
+
+    } catch (error) {
+        console.error("StartMailVerify controller error:", error);
+        res.status(500).json({
+            message: "internal-server-error",
+            code: 500
+        });
+    }
+};
+
 export const finishMailVerify = async (req: Request, res: Response): Promise<void> => {
     try {
         const {

+ 10 - 4
src/routes/authRoutes.ts

@@ -3,6 +3,7 @@ import {
 } from "express";
 import {
     finishMailVerify,
+    startMailVerify,
     refreshToken,
     register,
     logout,
@@ -10,7 +11,7 @@ import {
     me,
 } from "../controllers/authController";
 import {
-    authMiddleware, 
+    authMiddleware,
     AuthRequest
 } from "../middlewares/authMiddleware";
 
@@ -21,7 +22,7 @@ router.get("/me", authMiddleware, me);
 router.post("/register", register);
 router.post("/login", login);
 
-router.get("/validate-token", authMiddleware, (req:AuthRequest, res) => {
+router.get("/validate-token", authMiddleware, (req: AuthRequest, res) => {
     res.status(200)
         .json({
             message: "token-valid",
@@ -30,7 +31,12 @@ router.get("/validate-token", authMiddleware, (req:AuthRequest, res) => {
         });
 });
 
-router.post("/refresh-token", refreshToken);
 router.post("/finish-mail-verify", finishMailVerify);
+router.post("/start-mail-verify", startMailVerify);
+router.post("/refresh-token", refreshToken);
+
+export default router;
+
 
-export default router;
+// TODO PARAMS YANİ ARGS ALAN YERLERDE DTO OLARAK KONTROL EDİCEZ
+// TODO CONTEXT AUTHMİDDLEWARE EKLİYOR

+ 4 - 1
src/utils/index.ts

@@ -4,4 +4,7 @@ import {
 
 export const formatValidationErrors = (errors: ValidationError[]): string[] => {
     return errors.map(error => Object.values(error.constraints!)[0]);
-};
+};
+
+export const verificationCode = Math.floor(100000 + Math.random() * 900000).toString();
+export const TTL_SECONDS = 180;