Skip to content

Commit af1c584

Browse files
committed
feature : 로그아웃
1 parent aa07381 commit af1c584

8 files changed

Lines changed: 69 additions & 1 deletion

File tree

srcs/aichat/aichat.controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const createAiChatSession = async (req, res) => {
1717
export const handleAiChat = async (req, res) => {
1818
try {
1919
const { thread_id, message } = req.body;
20+
const userId = req.userId;
2021
if (!thread_id || !message) {
2122
return res.status(400).json(response(status.BAD_REQUEST, "thread_id와 메시지를 입력하세요"));
2223
}

srcs/aichat/aichat.route.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import express from "express";
22
import { createAiChatSession, handleAiChat } from "./aichat.controller.js";
3+
import authenticateToken from "../../config/jwt.middleware.js";
34

45
export const aichatRouter = express.Router();
56

7+
8+
aichatRouter.use(authenticateToken);
9+
10+
611
aichatRouter.post("/session", createAiChatSession); // 세션 생성
712
aichatRouter.post("/chat", handleAiChat); // 채팅
813
export default aichatRouter;

srcs/auth/auth.controller.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const handleKakaoAuth = async (req, res) => {
3838
try {
3939
const code = req.body.code || req.query.code;
4040

41+
//
42+
if (!code) {
43+
return res.json(response(
44+
{ isSuccess: status.BAD_REQUEST.isSuccess, code: 400, message: "인가 코드가 없습니다." },
45+
authErrorResponseDTO("인가 코드가 필요합니다.")
46+
));
47+
}
4148
const { accessToken, refreshToken, userInfo, isNewUser } = await kakaoLogin(code);
4249

4350
return res.status(isNewUser ? 201 : 200).json(response(

srcs/auth/auth.route.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
handleGoogleAuth,
77
handleTokenRefresh,
88
} from "./auth.controller.js";
9-
import { signupUser, loginUser } from "../user/user.controller.js";
9+
import { signupUser, loginUser, logout } from "../user/user.controller.js";
1010
import { authMiddleware } from "./auth.middleware.js";
11+
import authenticateToken from "../../config/jwt.middleware.js";
1112

1213
const authRouter = express.Router();
1314
authRouter.get("/verify", authMiddleware);
@@ -38,4 +39,8 @@ authRouter.post("/refresh", handleTokenRefresh);
3839
authRouter.post("/signUp", signupUser);
3940
authRouter.post("/login", loginUser);
4041

42+
authRouter.use(authenticateToken);
43+
44+
authRouter.post("/logout", authenticateToken, logout);
45+
4146
export default authRouter;

srcs/user/user.controller.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,31 @@ export const loginUser = async (req, res) => {
101101
console.log(error);
102102
res.send(response(status.BAD_REQUEST, "로그인 실패"));
103103
}
104+
};
105+
106+
export const logout = async (req, res) => {
107+
try {
108+
const userId = req.userId;
109+
console.log(userId);
110+
111+
if (!userId) {
112+
return res.status(401).json(response(
113+
{ isSuccess: false, code: 401, message: "로그인이 필요합니다." },
114+
{}
115+
));
116+
}
117+
118+
await UserService.logoutUser(userId);
119+
120+
return res.status(200).json(response(
121+
{ isSuccess: true, code: 200, message: "로그아웃 성공" },
122+
{}
123+
));
124+
} catch (error) {
125+
console.error("로그아웃 에러:", error);
126+
return res.status(500).json(response(
127+
{ isSuccess: false, code: 500, message: "로그아웃 중 오류 발생" },
128+
{}
129+
));
130+
}
104131
};

srcs/user/user.model.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,14 @@ export const UserModel = {
142142
throw new Error("프로필변경 실패");
143143
}
144144
},
145+
146+
userLogout: async (userId) => {
147+
try {
148+
await pool.query(sql.userLogoutSQL, [userId]);
149+
console.log(`사용자 ${userId} 로그아웃 완료`);
150+
} catch (error) {
151+
console.error("로그아웃 서비스 오류:", error);
152+
throw new Error("로그아웃 실패");
153+
}
154+
}
145155
};

srcs/user/user.service.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,16 @@ export const UserService = {
103103
throw new BaseError(status.BAD_REQUEST, "프로필 변경 실패");
104104
}
105105
},
106+
107+
logoutUser: async (userId) => {
108+
try {
109+
await UserModel.userLogout(userId);
110+
console.log(`사용자 ${userId} 로그아웃 완료`);
111+
} catch (error) {
112+
console.error("로그아웃 서비스 오류:", error);
113+
throw new Error("로그아웃 실패");
114+
}
115+
}
116+
117+
106118
};

srcs/user/user.sql.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export const sql = {
1010
postRefreshToken: `UPDATE user SET refresh_token = ? WHERE user_id = ?`,
1111
logoutUserSQL: `UPDATE user SET refresh_token = ? WHERE user_id = ?`,
1212
updateProfileSQL: `UPDATE user SET profile_image_url = ? WHERE user_id = ?`,
13+
userLogoutSQL: `UPDATE user SET refresh_token = NULL WHERE user_id = ?`,
1314
};

0 commit comments

Comments
 (0)