-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor: 인터셉터 리프레쉬 토큰 서버사이드 렌더링 코드 추가 #74
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
summerDev96
merged 11 commits into
codeit-part3-7:dev
from
summerDev96:refactor/interceptor
Jul 30, 2025
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bc6fdd5
feat: 인터셉터 서버사이드 렌더링 코드 추가
summerDev96 9743689
feat: 인터셉터 리프레쉬 토큰 서버사이드 렌더링 로직 추가
summerDev96 943daac
feat: 인터셉터 apiClient export 추가
summerDev96 8e361e9
fix: 절대 경로 import로 수정
summerDev96 c32d0b3
chore: 미사용 import 삭제
summerDev96 0f3d17c
Merge branch 'dev' of https://github.com/codeit-part3-7/WHYNE_FE into…
summerDev96 ba333a9
fix: 토큰 체크하는 check-token API 추가
summerDev96 8938eb9
fix: 린트 오류 수정
summerDev96 5eafbe9
Merge branch 'dev' of https://github.com/codeit-part3-7/WHYNE_FE into…
summerDev96 06fa35f
fix: 빌드 타입 오류 수정
summerDev96 3abc26d
fix: 빌드 star 오류 해결
summerDev96 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import apiClient from '@/api/apiClient'; | ||
| import { GetServerSidePropsContext } from 'next'; | ||
|
|
||
| import { GetUserResponse } from '@/types/UserTypes'; | ||
|
|
||
| export const getUser = (): Promise<GetUserResponse> => { | ||
| return apiClient.get(`/${process.env.NEXT_PUBLIC_TEAM}/users/me`); | ||
| import { createApiClient } from './apiClient'; | ||
|
|
||
| // getServerSideProps 확인을 위해 cookieHeader 부분 임시 추가 | ||
| export const getUser = (context?: GetServerSidePropsContext): Promise<GetUserResponse> => { | ||
| return createApiClient(context).get(`/${process.env.NEXT_PUBLIC_TEAM}/users/me`); | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { | ||
| ClearAuthCookiesParams, | ||
| GetClientCookieParams, | ||
| GetCookieParams, | ||
| GetServerCookieParams, | ||
| GetServerCookieReturn, | ||
| SetCookieCallbackParams, | ||
| SetCookieParams, | ||
| SetCookieType, | ||
| SetServerCookieParams, | ||
| } from '@/types/CookieTypes'; | ||
|
|
||
| import { isClient } from './utils'; | ||
|
|
||
| export function getCookie({ cookieHeader, name }: GetCookieParams) { | ||
| return isClient() ? getClientCookie({ name }) : getServerCookie({ cookieHeader, name }); | ||
| } | ||
|
|
||
| export function setCookie({ response, name, value, maxAge }: SetCookieParams) { | ||
| if (isClient()) { | ||
| return setClientCookie({ name, value, maxAge }); | ||
| } | ||
| if (response) { | ||
| return setServerCookie({ response, name, value, maxAge }); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export function getClientCookie({ name }: GetClientCookieParams) { | ||
| const cookieArr = document.cookie.split('; '); | ||
| for (const cookie of cookieArr) { | ||
| const [key, value] = cookie.split('='); | ||
| if (key === name) return decodeURIComponent(value); | ||
| } | ||
| } | ||
|
|
||
| export function getServerCookie({ | ||
| cookieHeader, | ||
| name, | ||
| }: GetServerCookieParams): GetServerCookieReturn { | ||
| if (!cookieHeader) return undefined; | ||
|
|
||
| const cookies = cookieHeader.split(';'); | ||
| for (const cookie of cookies) { | ||
| const [key, ...val] = cookie.trim().split('='); | ||
| if (key === name) { | ||
| return decodeURIComponent(val.join('=')); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function setClientCookie({ name, value, maxAge }: SetCookieType) { | ||
| document.cookie = `${name}=${encodeURIComponent(value)}; path=/; max-age=${maxAge}; SameSite=Lax; Secure`; | ||
| } | ||
|
|
||
| export function setServerCookie({ response, name, value, maxAge }: SetServerCookieParams) { | ||
| const cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAge}; SameSite=Lax; Secure; HttpOnly`; | ||
|
|
||
| const prevCookies = response.getHeader('Set-Cookie'); | ||
|
|
||
| if (!prevCookies) { | ||
| response.setHeader('Set-Cookie', cookie); | ||
| } else if (Array.isArray(prevCookies)) { | ||
| response.setHeader('Set-Cookie', [...prevCookies, cookie]); | ||
| } else if (typeof prevCookies === 'string') { | ||
| response.setHeader('Set-Cookie', [prevCookies, cookie]); | ||
| } | ||
| } | ||
|
|
||
| export function setAuthCookiesWithCallback({ | ||
| accessToken, | ||
| refreshToken, | ||
| callback, | ||
| }: SetCookieCallbackParams) { | ||
| setCookie({ name: 'accessToken', value: accessToken, maxAge: 1800 }); // 만료 30분 | ||
| setCookie({ name: 'refreshToken', value: refreshToken, maxAge: 604800 }); // 만료 7일 | ||
| if (isClient()) { | ||
| callback(); | ||
| } | ||
| } | ||
|
|
||
| export function clearAuthCookiesWithCallback(callback: ClearAuthCookiesParams) { | ||
| setCookie({ name: 'accessToken', value: '', maxAge: 0 }); | ||
| setCookie({ name: 'refreshToken', value: '', maxAge: 0 }); | ||
| if (isClient()) { | ||
| callback(); | ||
| } | ||
| } | ||
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
고생하셨습니다!
테스트용 코드랑 전체적으로 큰 흐름은 비슷한 것 같습니다
함수를 어떻게 분리했는지 이런 세세한 것만 조금 다른 것 같아요
근데 여기서 httpOnly를 추가해서 보내버리면
겟 서버사이드 프롭스를 통해 이걸 사용한 페이지가 렌더 된 후
->
나중에 로그아웃할 때 로컬에서 강제로 쿠키에 접근해 삭제해야 할텐데 문제 없이 접근할 수 있나요???
저도 잘 모르겠어서 멘토님께 여쭤보고 피드백 받거나
직접 실험해봐야지 확실히 알 수 있을 거 같습니다.
그리고 SameSite =Lax
-> 이것도 다른 사이트에서 이미지를 가져올 때(get) 쿠키를 안보낸다고 하는데
-> 오늘 이미지 화면에 뿌려보니까 s3 아마존 어쩌구에서 받아서 오더라구요?
(sprint-fe-project.s3.ap-northeast-2.amazonaws.com)
이 경우에 안 걸리는지도 확인해봐야 할 것 같아요.
->만약 걸려서 요청이 제대로 안 보내진다면? none으로 설정해야 할텐데... 다른 방법이 있는지 여쭤보는 것도 좋을 것 같습니다