Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

사용자 인증절차 강화 #47

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const isAuthenticated = require("./middlewares/auth"); // 로그인 여부 미

app.use(
cors({
origin: ["localhost:5173", "modonggu.site"],
origin: ["http://localhost:5173", "https://modonggu.site"],
credentials: true,
})
);
Expand Down
30 changes: 23 additions & 7 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
"use strict";

module.exports = function isAuthenticated(req, res, next) {
if (req.session && req.session.user) {
// 세션이 존재하고 사용자 정보가 있으면 인증된 것으로 간주
next();
} else {
// 인증되지 않은 경우
res.status(401).json({ message: "인증이 필요합니다." });
const userStorage = require("../storages/userStorage");

module.exports = async function isAuthenticated(req, res, next) {
try {
console.log(req.sessionID);
if (req.session && req.session.user) {
// 세션 ID를 사용하여 DB에서 세션 정보 조회
const isSessionValid = await userStorage.getSessionInfo(req.sessionID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 sessions 테이블을 새로 만드신건가요??

찾아보니까 명규님이 말씀하신대로 DB에 세션 정보 저장을 안해도 된다고 봐서요 !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session 테이블은 mysql-session 사용을 하면 자동으로 테이블이 생성되고, app.js에서 설정한 대로 db에 자동으로 들어가고 만료되면 사라지는 구조로 자동 생성 되는걸로 알고있습니다 !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와우 그런 구조가 ... 신기하네요 !!
알겠습니다 ~


if (isSessionValid) {
// 세션이 유효하면 인증된 것으로 간주
next();
} else {
// 세션 정보가 없으면 인증되지 않은 것으로 간주
res.status(401).json({ message: "로그인이 필요합니다." });
}
} else {
// 세션이 없거나 사용자 정보가 없으면 인증되지 않은 것으로 간주
res.status(401).json({ message: "로그인이 필요합니다." });
}
} catch (error) {
console.error("인증 중 오류:", error);
res.status(500).json({ message: "서버 오류" });
}
};
16 changes: 16 additions & 0 deletions storages/userStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ class UserStorage {
throw new Error("프로필 이미지 삭제 중 오류가 발생했습니다.");
}
}

static async getSessionInfo(sessionId) {
try {
const query = "SELECT * FROM sessions WHERE session_id = ?";
const [results] = await db.execute(query, [sessionId]);

if (!results || results.length === 0) {
return false;
}

return true;
} catch (error) {
console.error("세션 정보 조회 중 오류:", error);
throw error;
}
}
}

module.exports = UserStorage;