-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: 후기 페이지 서버에서 주어지는 데이터에 맞게 props 변경 * feat: recoil, recoil-persist 패키지 다운로드 * feat: 후기 페이지 API 연결 * feat: 후기 페이지 API 연결 * feat: axios interceptor를 사용해서 API 요청 전 토큰 재발급 * feat: 리코일 사용을 위해 설정 * feat: 헤더에 로그인 버튼 추가 * feat: 로그인을 위한 함수 구현 * feat: 로그인 성공시 사용자의 정보를 recoil에 저장 * feat: 후기 페이지 API 연결
- Loading branch information
Showing
14 changed files
with
369 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,22 @@ | ||
import Axios from '..'; | ||
|
||
export const fetchBoardData = | ||
(boardType: string, filter: string, page: number, size: number) => () => { | ||
if (boardType === 'review') { | ||
return Axios.get('/reviews/category', { | ||
params: { | ||
category: filter === '전체' ? null : filter, | ||
page: page - 1, | ||
size, | ||
}, | ||
}); | ||
} else if (boardType === 'archive') { | ||
return Axios.get('/archives/category', { | ||
params: { | ||
category: filter, | ||
page: page - 1, | ||
size, | ||
}, | ||
}); | ||
} | ||
}; |
This file contains 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,7 +1,81 @@ | ||
import axios from 'axios'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
import Cookies from 'js-cookie'; | ||
import { onLoginSuccess } from '@/pages/login/functions'; | ||
|
||
const Axios = axios.create(); | ||
Axios.defaults.baseURL = 'http://52.78.13.36'; // 서버 URL | ||
const ServerURL = 'https://www.tvmaker.shop'; // 서버 URL | ||
Axios.defaults.baseURL = ServerURL; | ||
Axios.defaults.withCredentials = true; | ||
|
||
// 요청 전 access 토큰 만료되었는지 확인 | ||
Axios.interceptors.request.use( | ||
async config => { | ||
const expireToken = localStorage.getItem('expireToken'); | ||
const expireTime = expireToken | ||
? new Date(expireToken as string) | ||
: undefined; | ||
const currentTime = new Date().getTime(); | ||
const refreshToken = Cookies.get('refreshToken'); | ||
|
||
if (expireTime && refreshToken) { | ||
// 이전에 로그인한 적이 있음 | ||
if (currentTime > expireTime.getTime()) { | ||
// 만료되었으면 | ||
const res = await axios.get(`${ServerURL}/auth/refresh`, { | ||
params: { | ||
refreshToken, | ||
}, | ||
}); | ||
const { accessToken: newAccessToken, refreshToken: newRefreshToken } = | ||
res?.data?.result; | ||
|
||
// header에 accessToken 세팅 | ||
Axios.defaults.headers.common[ | ||
'Authorization' | ||
] = `Bearer ${newAccessToken}`; | ||
config.headers.Authorization = `Bearer ${newAccessToken}`; | ||
|
||
// 갱신된 만료 시간 localStorage에 저장 | ||
const newExpireTime = new Date(currentTime + 1 * 60 * 60 * 1000); // 유효시간 1시간 | ||
localStorage.setItem('expireToken', newExpireTime.toString()); | ||
|
||
Cookies.remove('refreshToken'); | ||
Cookies.set('refreshToken', newRefreshToken, { expires: 1 }); | ||
} | ||
} | ||
return config; | ||
}, | ||
error => Promise.reject(error), | ||
); | ||
|
||
// 응답 전 accessToken이 만료되어 발생한 에러라면 갈아끼우고 다시 요청 | ||
Axios.interceptors.response.use( | ||
(response: AxiosResponse) => { | ||
return response; | ||
}, | ||
async error => { | ||
if (error.response?.status === 401) { | ||
// accessToken이 없어서 에러가 발생한 상황 | ||
const refreshToken = Cookies.get('refreshToken'); | ||
if (refreshToken) { | ||
// refreshToken이 있다면, 이미 로그인된 사용자이므로 | ||
const res = await axios.get(`${ServerURL}/auth/refresh`, { | ||
params: { refreshToken }, | ||
}); | ||
// 기존에 있던 refreshToken은 지우고 | ||
Cookies.remove('refreshToken'); | ||
// 로그인 성공 로직 실행 | ||
onLoginSuccess(res, null); | ||
return Axios(error.config); | ||
} else { | ||
// refreshToken이 없다는 건 로그인을 해야 한다는 것 | ||
if (window.confirm('로그인이 필요한 서비스입니다.')) { | ||
window.location.href = '/login'; | ||
} | ||
} | ||
} | ||
return error; | ||
}, | ||
); | ||
|
||
export default Axios; |
This file contains 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 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.