소스 검색

Bugfix: Refactor logout and me actions to use context parameter for user authentication

BedirhanOZCAN 1 개월 전
부모
커밋
0920e98c7a
5개의 변경된 파일55개의 추가작업 그리고 51개의 파일을 삭제
  1. 26 9
      src/actions/auth/logout/index.ts
  2. 6 1
      src/actions/auth/logout/types.ts
  3. 13 4
      src/actions/auth/me/index.ts
  4. 5 0
      src/actions/auth/me/types.ts
  5. 5 37
      src/controllers/authController.ts

+ 26 - 9
src/actions/auth/logout/index.ts

@@ -1,18 +1,37 @@
+import {
+    LogoutResult,
+    ContextType
+} from "./types";
 import {
     User
 } from "../../../models/User";
 import redis from "../../../config/redis";
-import {
-    LogoutResult
-} from "./types";
 
-const logout = async (userID: string, token: string): Promise<LogoutResult> => {
+export const logout = async (context?: ContextType): Promise<LogoutResult> => {
     try {
+        if (!context) {
+            return {
+                message: "unauthorized-missing-context",
+                code: 401
+            };
+        }
+
+        const {
+            userID, token
+        } = context;
+
+        if (!userID || !token) {
+            return {
+                message: "unauthorized-missing-user-information",
+                code: 401
+            };
+        }
+
         await User.findByIdAndUpdate(userID, {
             refreshToken: null
         });
 
-        await redis.del(`${userID}`);
+        await redis.del(`user:${userID}`);
 
         return {
             message: "logout-successful",
@@ -21,10 +40,8 @@ const logout = async (userID: string, token: string): Promise<LogoutResult> => {
     } catch (error) {
         console.error("Logout action error:", error);
         return {
-            message: "logout-failed",
+            message: "server-error",
             code: 500,
         };
     }
-};
-
-export default logout;
+};

+ 6 - 1
src/actions/auth/logout/types.ts

@@ -1,4 +1,9 @@
 export interface LogoutResult {
     message: string;
     code: number;
-}
+}
+
+export interface ContextType {
+    userID: string;
+    token: string;
+}

+ 13 - 4
src/actions/auth/me/index.ts

@@ -2,12 +2,19 @@ import {
     User
 } from "../../../models/User";
 import {
-    MeResult
+    ContextType, MeResult
 } from "./types";
 
-const me = async (userID: string): Promise<MeResult> => {
+const me = async (context?: ContextType): Promise<MeResult> => {
     try {
-        const user = await User.findById(userID, {
+        if (!context || !context.userID) {
+            return {
+                message: "unauthorized",
+                code: 401,
+            };
+        }
+
+        const user = await User.findById(context.userID, {
             phoneNumber: 1,
             companyName: 1,
             firstName: 1,
@@ -16,6 +23,7 @@ const me = async (userID: string): Promise<MeResult> => {
             mail: 1,
             _id: 1,
         });
+
         if (!user) {
             return {
                 message: "user-not-found",
@@ -41,7 +49,8 @@ const me = async (userID: string): Promise<MeResult> => {
     } catch (error) {
         console.error("Me action error:", error);
         return {
-            message: "internal-server-error", code: 500
+            message: "internal-server-error",
+            code: 500
         };
     }
 };

+ 5 - 0
src/actions/auth/me/types.ts

@@ -12,4 +12,9 @@ export interface MeResult {
             mail: string;
         };
     };
+}
+
+export interface ContextType {
+    userID: string;
+    token: string;
 }

+ 5 - 37
src/controllers/authController.ts

@@ -82,30 +82,7 @@ export const login = async (req: Request, res: Response): Promise<void> => {
 
 export const logout = async (req: AuthRequest, res: Response): Promise<void> => {
     try {
-        const context = req.context;
-
-        if (!context) {
-            res.status(401).json({
-                message: "unauthorized-missing-context",
-                code: 401
-            });
-            return;
-        }
-
-        const {
-            userID,
-            token
-        } = context;
-
-        if (!userID || !token) {
-            res.status(401).json({
-                message: "unauthorized-missing-user-information",
-                code: 401
-            });
-            return;
-        }
-
-        const result = await _logout(userID, token);
+        const result = await _logout(req.context);
 
         res.status(result.code).json({
             message: result.message,
@@ -122,16 +99,7 @@ export const logout = async (req: AuthRequest, res: Response): Promise<void> =>
 
 export const me = async (req: AuthRequest, res: Response): Promise<void> => {
     try {
-        const context = req.context;
-
-        if (!context || !context.userID) {
-            res.status(401).json({
-                message: "unauthorized", code: 401
-            });
-            return;
-        }
-
-        const result = await _me(context.userID);
+        const result = await _me(req.context);
 
         res.status(result.code).json({
             message: result.message,
@@ -177,12 +145,12 @@ export const finishMailVerify = async (req: Request, res: Response): Promise<voi
     try {
         const {
             userID,
-            code 
+            code
         } = req.body;
 
         const result = await _finishMailVerify({
-            userID, 
-            code 
+            userID,
+            code
         });
 
         res.status(result.code).json({