-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor/103 #104
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
Merged
Merged
Refactor/103 #104
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1cd30b
refactor: 토큰을 스토어에 저장하는것 제거
Han-wo 61ed013
feat: 편의를 위한 getter 함수 추가
Han-wo d9086b7
feat: 서버액션 로그인 설정
Han-wo 774d47f
feat: 서버액션에 세션인증관련 로직 추가
Han-wo 2411d2b
refactor: 토큰 관련 로직 제거 및 쿠키 포함으로 변경
Han-wo e8a207b
refactor: 나머지 컴포넌트 수정
Han-wo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| "use server"; | ||
|
|
||
| import { cookies } from "next/headers"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| interface LoginCredentials { | ||
| memberEmail: string; | ||
| memberPassword: string; | ||
| } | ||
|
|
||
| interface LoginResponse { | ||
| token: string; | ||
| memberId: number; | ||
| memberName: string; | ||
| memberNickName: string; | ||
| annualIncome: number; | ||
| deposit: number; | ||
| } | ||
|
|
||
| interface AuthActionResult { | ||
| success: boolean; | ||
| error?: string; | ||
| data?: { | ||
| memberId: string; | ||
| memberName: string; | ||
| memberNickName: string; | ||
| annualIncome: string; | ||
| deposit: string; | ||
| }; | ||
| } | ||
|
|
||
| interface SessionResult { | ||
| success: boolean; | ||
| error?: string; | ||
| } | ||
|
|
||
| // 공통 쿠키 옵션 | ||
| const getCookieOptions = () => ({ | ||
| httpOnly: true, | ||
| secure: process.env.NODE_ENV === "production", | ||
| sameSite: "lax" as const, | ||
| maxAge: 30 * 60, // 30분 | ||
| path: "/", | ||
| }); | ||
|
|
||
| /** | ||
| * 로그인 서버 액션 | ||
| * @param credentials - 로그인 자격 증명 (이메일, 비밀번호) | ||
| * @returns 로그인 결과 (성공/실패, 사용자 정보) | ||
| */ | ||
| export async function loginAction( | ||
| credentials: LoginCredentials, | ||
| ): Promise<AuthActionResult> { | ||
| try { | ||
| const response = await fetch( | ||
| `${process.env.NEXT_PUBLIC_API_URL}/api/login`, | ||
| { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(credentials), | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response.json(); | ||
| return { | ||
| success: false, | ||
| error: errorData.message || "로그인에 실패했습니다.", | ||
| }; | ||
| } | ||
|
|
||
| const data: LoginResponse = await response.json(); | ||
|
|
||
| // 서버에서만 httpOnly 쿠키 설정 | ||
| const cookieStore = cookies(); | ||
| const cookieOptions = getCookieOptions(); | ||
|
|
||
| // 모든 인증 정보를 httpOnly 쿠키로 저장 | ||
| cookieStore.set("token", data.token, cookieOptions); | ||
| cookieStore.set("memberId", data.memberId.toString(), cookieOptions); | ||
| cookieStore.set("memberName", data.memberName, cookieOptions); | ||
| cookieStore.set("memberNickName", data.memberNickName, cookieOptions); | ||
| cookieStore.set( | ||
| "annualIncome", | ||
| data.annualIncome.toString(), | ||
| cookieOptions, | ||
| ); | ||
| cookieStore.set("deposit", data.deposit.toString(), cookieOptions); | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| memberId: data.memberId.toString(), | ||
| memberName: data.memberName, | ||
| memberNickName: data.memberNickName, | ||
| annualIncome: data.annualIncome.toString(), | ||
| deposit: data.deposit.toString(), | ||
| }, | ||
| }; | ||
| } catch (error) { | ||
| console.error("로그인 서버 액션 오류:", error); //eslint-disable-line | ||
| return { | ||
| success: false, | ||
| error: "로그인 중 오류가 발생했습니다.", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 로그아웃 서버 액션 | ||
| * 모든 인증 관련 쿠키를 삭제하고 로그인 페이지로 리다이렉트 | ||
| */ | ||
| export async function logoutAction(): Promise<void> { | ||
| try { | ||
| const cookieStore = cookies(); | ||
|
|
||
| // 모든 인증 관련 쿠키 삭제 | ||
| const cookiesToDelete = [ | ||
| "token", | ||
| "memberId", | ||
| "memberName", | ||
| "memberNickName", | ||
| "annualIncome", | ||
| "deposit", | ||
| ]; | ||
|
|
||
| cookiesToDelete.forEach((cookieName) => { | ||
| cookieStore.delete(cookieName); | ||
| }); | ||
| } catch (error) { | ||
| console.error("로그아웃 중 오류:", error); //eslint-disable-line | ||
| } finally { | ||
| // 오류가 발생해도 로그인 페이지로 리다이렉트 | ||
| redirect("/login"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 세션 연장 서버 액션 | ||
| * 기존 쿠키들의 만료시간을 연장 (30분) | ||
| * @returns 세션 연장 결과 | ||
| */ | ||
| export async function extendSessionAction(): Promise<SessionResult> { | ||
| try { | ||
| const cookieStore = cookies(); | ||
| const token = cookieStore.get("token")?.value; | ||
|
|
||
| if (!token) { | ||
| return { | ||
| success: false, | ||
| error: "인증 토큰이 없습니다.", | ||
| }; | ||
| } | ||
|
|
||
| // 기존 사용자 정보 가져오기 | ||
| const userInfo = { | ||
| token, | ||
| memberId: cookieStore.get("memberId")?.value, | ||
| memberName: cookieStore.get("memberName")?.value, | ||
| memberNickName: cookieStore.get("memberNickName")?.value, | ||
| annualIncome: cookieStore.get("annualIncome")?.value, | ||
| deposit: cookieStore.get("deposit")?.value, | ||
| }; | ||
|
|
||
| // 필수 정보가 없으면 실패 | ||
| if (!userInfo.memberId) { | ||
| return { | ||
| success: false, | ||
| error: "사용자 정보가 없습니다.", | ||
| }; | ||
| } | ||
|
|
||
| // 새로운 만료시간으로 쿠키 재설정 | ||
| const cookieOptions = getCookieOptions(); | ||
|
|
||
| cookieStore.set("token", userInfo.token, cookieOptions); | ||
| cookieStore.set("memberId", userInfo.memberId, cookieOptions); | ||
| cookieStore.set("memberName", userInfo.memberName || "", cookieOptions); | ||
| cookieStore.set( | ||
| "memberNickName", | ||
| userInfo.memberNickName || "", | ||
| cookieOptions, | ||
| ); | ||
| cookieStore.set("annualIncome", userInfo.annualIncome || "", cookieOptions); | ||
| cookieStore.set("deposit", userInfo.deposit || "", cookieOptions); | ||
|
|
||
| return { success: true }; | ||
| } catch (error) { | ||
| console.error("세션 연장 중 오류:", error); //eslint-disable-line | ||
| return { | ||
| success: false, | ||
| error: "세션 연장 중 오류가 발생했습니다.", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 사용자 정보 업데이트 서버 액션 | ||
| * 특정 사용자 정보만 업데이트 (예: 예수금 변경) | ||
| * @param updates - 업데이트할 정보 | ||
| */ | ||
| export async function updateUserInfoAction(updates: { | ||
| deposit?: string; | ||
| annualIncome?: string; | ||
| }): Promise<SessionResult> { | ||
| try { | ||
| const cookieStore = cookies(); | ||
| const token = cookieStore.get("token")?.value; | ||
|
|
||
| if (!token) { | ||
| return { | ||
| success: false, | ||
| error: "인증 토큰이 없습니다.", | ||
| }; | ||
| } | ||
|
|
||
| const cookieOptions = getCookieOptions(); | ||
|
|
||
| // 업데이트할 정보만 새로 설정 | ||
| if (updates.deposit !== undefined) { | ||
| cookieStore.set("deposit", updates.deposit, cookieOptions); | ||
| } | ||
|
|
||
| if (updates.annualIncome !== undefined) { | ||
| cookieStore.set("annualIncome", updates.annualIncome, cookieOptions); | ||
| } | ||
|
|
||
| console.log("사용자 정보 업데이트 완료:", updates); //eslint-disable-line | ||
|
|
||
| return { success: true }; | ||
| } catch (error) { | ||
| console.error("사용자 정보 업데이트 중 오류:", error); //eslint-disable-line | ||
| return { | ||
| success: false, | ||
| error: "사용자 정보 업데이트 중 오류가 발생했습니다.", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 토큰 유효성 검증 서버 액션 | ||
| * @returns 토큰 유효성 결과 | ||
| */ | ||
| export async function validateTokenAction(): Promise<SessionResult> { | ||
| try { | ||
| const cookieStore = cookies(); | ||
| const token = cookieStore.get("token")?.value; | ||
|
|
||
| if (!token) { | ||
| return { | ||
| success: false, | ||
| error: "토큰이 없습니다.", | ||
| }; | ||
| } | ||
|
|
||
| // 실제 구현에서는 JWT 토큰 검증 로직 추가 | ||
| // const isValid = jwt.verify(token, process.env.JWT_SECRET); | ||
|
|
||
|
Comment on lines
+258
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JWT 토큰 검증 구현 필요 토큰 존재 여부만 확인하고 실제 JWT 검증이 구현되지 않은 것은 심각한 보안 취약점입니다. 유효하지 않거나 만료된 토큰도 통과할 수 있습니다. JWT 검증 로직을 구현하는 코드를 생성하거나 이를 추적할 새 이슈를 생성할까요? 🤖 Prompt for AI Agents |
||
| // 현재는 토큰 존재 여부만 확인 | ||
| return { success: true }; | ||
| } catch (error) { | ||
| console.error("토큰 검증 중 오류:", error); //eslint-disable-line | ||
| return { | ||
| success: false, | ||
| error: "토큰 검증에 실패했습니다.", | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쿠키 삭제 시 옵션 지정 필요
쿠키를 삭제할 때 설정 시와 동일한 옵션(특히 path)을 지정해야 올바르게 삭제됩니다:
cookiesToDelete.forEach((cookieName) => { - cookieStore.delete(cookieName); + cookieStore.delete({ + name: cookieName, + path: "/", + }); });🤖 Prompt for AI Agents