-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: axios 세팅 #13
Merged
Merged
feat: axios 세팅 #13
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
379ab35
feat: axios 세팅
ShinjungOh 3ba1310
feat: 로그인, 회원가입 기능 구현
ShinjungOh ece1d69
feat: 로컬스토리지를 이용한 엑세스 토큰 처리
ShinjungOh 5e382d2
feat: 로그아웃 버튼 구현
ShinjungOh eff4a10
refactor: axios interceptor 설정 및 구조 개선
ShinjungOh a7b2242
refacotr: 로그인, 회원가입 로직 개선
ShinjungOh 7821c12
refactor: API 요청 응답 타입 지정 및 로컬스토리지 관련 코드 개선
ShinjungOh 8cd43f7
feat: 카카오 소셜 로그인 기능 구현
ShinjungOh 8716487
fix: 카카오 로그인 함수 useCallback 코드 개선
ShinjungOh 787845f
feat: 카카오 로그인 실패 처리 구현
ShinjungOh 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 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
Empty file.
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,13 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="images/icon/menu/feel-cat.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>하루한냥</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
<html lang='en'> | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<link rel='icon' type='image/svg+xml' href='images/icon/menu/feel-cat.svg' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | ||
<title>하루한냥</title> | ||
</head> | ||
<body> | ||
<div id='root'></div> | ||
<script type='module' src='/src/main.tsx'></script> | ||
</body> | ||
</html> |
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,65 @@ | ||
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'; | ||
import qs from 'qs'; | ||
import { API_TIMEOUT, BASE_URL } from '@lib/const/config'; | ||
import { ACCESS_TOKEN } from '@lib/const/localstorage'; | ||
|
||
type APIResponse<T = unknown> = { | ||
success: boolean; | ||
msg: string; | ||
data?: T; | ||
}; | ||
|
||
export const client = axios.create({ | ||
baseURL: BASE_URL, | ||
timeout: API_TIMEOUT, | ||
timeoutErrorMessage: '서버 요청 시간 초과', | ||
paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'comma' }), | ||
}); | ||
|
||
const handleHeadersWithAccessToken = (config: AxiosRequestConfig): InternalAxiosRequestConfig => { | ||
const accessToken = localStorage.getItem(ACCESS_TOKEN) || ''; | ||
config.headers = { | ||
...config.headers, | ||
Authorization: `Bearer ${accessToken}`, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
return config as InternalAxiosRequestConfig; | ||
}; | ||
|
||
client.interceptors.request.use(handleHeadersWithAccessToken); | ||
client.interceptors.response.use( | ||
(response: AxiosResponse) => response, | ||
(error: AxiosError) => Promise.reject(error), | ||
); | ||
|
||
export const http = { | ||
get: function get<Response = unknown>(url: string, config?: AxiosRequestConfig) { | ||
return client.get<APIResponse<Response>>(url, config).then((res) => res.data); | ||
}, | ||
post: function post<Response = unknown, Request = any>(url: string, data?: Request, config?: AxiosRequestConfig) { | ||
return client.post<APIResponse<Response>>(url, data, config).then((res) => res.data); | ||
}, | ||
put: function put<Response = unknown, Request = any>(url: string, data?: Request, config?: AxiosRequestConfig) { | ||
return client.put<APIResponse<Response>>(url, data, config).then((res) => res.data); | ||
}, | ||
patch: function patch<Response = unknown, Request = any>(url: string, data?: Request, config?: AxiosRequestConfig) { | ||
return client.patch<APIResponse<Response>>(url, data, config).then((res) => res.data); | ||
}, | ||
delete: function del<Response = unknown>(url: string, config?: AxiosRequestConfig) { | ||
return client.delete<APIResponse<Response>>(url, config).then((res) => res.data); | ||
}, | ||
}; | ||
|
||
export const handleAxiosError = (error: any) => { | ||
if (axios.isAxiosError(error)) { | ||
const axiosError = error as AxiosError; | ||
console.error('Axios Error:', axiosError); | ||
return axiosError.response?.data as APIResponse; | ||
} | ||
console.log('Unknown Error: ', error); | ||
return { | ||
success: false, | ||
msg: '알 수 없는 오류가 발생했습니다.', | ||
}; | ||
}; |
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,6 @@ | ||
export const BASE_URL = 'http://133.186.144.153:3001/api'; | ||
export const API_TIMEOUT = 3000; | ||
|
||
const KAKAO_CLIENT_ID = 'a3d08c94732b0c85334d04b474f49873'; | ||
const KAKAO_REDIRECT_URL = 'http://localhost:5173/oauth/kakao'; | ||
export const KAKAO_AUTH_URL = `https://kauth.kakao.com/oauth/authorize?client_id=${KAKAO_CLIENT_ID}&redirect_uri=${KAKAO_REDIRECT_URL}&response_type=code`; | ||
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,2 @@ | ||
export const ACCESS_TOKEN = 'accessToken'; | ||
export const USER = 'user'; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export type User = { | ||
export type UserType = { | ||
email: string; | ||
password: string; | ||
passwordCheck: string; | ||
|
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,40 @@ | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useCallback, useEffect } from 'react'; | ||
import { useNavigate } from 'react-router'; | ||
import { PATH } from '@lib/const/path'; | ||
import { ACCESS_TOKEN, USER } from '@lib/const/localstorage'; | ||
import { handleAxiosError, http } from '../api/http'; | ||
|
||
export default function AuthKakaoPage() { | ||
const navigate = useNavigate(); | ||
const [params] = useSearchParams(); | ||
const code = params.get('code'); | ||
|
||
const handleSigninKakao = useCallback(async () => { | ||
try { | ||
const responseSignIn = await http.post<{ token: string; user: { name: string } }>('/user/oauth/kakao', { code }); | ||
const isSuccess = responseSignIn.success; | ||
if (isSuccess && responseSignIn.data) { | ||
const accessToken = responseSignIn.data.token; | ||
const userProfile = { | ||
name: responseSignIn.data.user.name, | ||
}; | ||
localStorage.setItem(ACCESS_TOKEN, JSON.stringify(accessToken)); | ||
localStorage.setItem(USER, JSON.stringify(userProfile)); | ||
navigate(PATH.CALENDAR); | ||
} | ||
} catch (e) { | ||
const error = handleAxiosError(e); | ||
alert(error.msg); | ||
navigate(PATH.HOME); | ||
} | ||
}, [code, navigate]); | ||
|
||
useEffect(() => { | ||
if (code) { | ||
handleSigninKakao(); | ||
} | ||
}, [code, handleSigninKakao]); | ||
|
||
return <div />; | ||
} |
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 |
---|---|---|
@@ -1,13 +1,56 @@ | ||
import Header from '@ui/components/layout/Header'; | ||
import Body from '@ui/components/layout/Body'; | ||
import Menu from '@ui/components/layout/Menu'; | ||
import SignButton from '@ui/components/SignButton'; | ||
import styleTokenCss from '@ui/styles/styleToken.css'; | ||
import styled from '@emotion/styled'; | ||
import { useNavigate } from 'react-router'; | ||
import { PATH } from '@lib/const/path'; | ||
|
||
export default function SettingPage() { | ||
const navigate = useNavigate(); | ||
|
||
const handleClickLogout = () => { | ||
const isLogout = confirm('로그아웃 하시겠습니까?'); | ||
if (isLogout) { | ||
localStorage.clear(); | ||
navigate(PATH.HOME); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
<Body>setting page</Body> | ||
<Title>마이 페이지</Title> | ||
<Container> | ||
<SignButton | ||
text="로그아웃" | ||
backgroundColor={styleTokenCss.color.secondaryActive} | ||
color={styleTokenCss.color.white} | ||
onClick={handleClickLogout} | ||
/> | ||
</Container> | ||
<Menu /> | ||
</> | ||
); | ||
} | ||
|
||
const Title = styled.h2` | ||
background-color: ${styleTokenCss.color.background}; | ||
color: ${styleTokenCss.color.gray2}; | ||
font-size: 24px; | ||
font-weight: 600; | ||
|
||
display: flex; | ||
flex-direction: row; | ||
justify-content: flex-start; | ||
align-items: center; | ||
width: 100%; | ||
height: 100px; | ||
padding: 35px; | ||
`; | ||
|
||
const Container = styled(Body)` | ||
padding: 35px; | ||
justify-content: flex-start; | ||
align-items: center; | ||
overflow-y: auto; | ||
`; |
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.
constant를 변수로 잘 분리해주셨네요. 👍