Skip to content

Commit 6860df7

Browse files
authored
Merge pull request #15 from DevKor-github/feat/#3/auth-client
Feat/#3/auth client
2 parents 4e170ed + 8ac8446 commit 6860df7

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import useGetIsLogin from '@/common/hooks/apis/useGetIsLogin';
2+
3+
import * as s from './style.css';
4+
5+
const TestAPIButton = () => {
6+
const { refetch } = useGetIsLogin();
7+
return (
8+
<button className={s.Button} onClick={() => refetch()}>
9+
API 테스팅하기
10+
</button>
11+
);
12+
};
13+
export default TestAPIButton;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { css } from '@styled-system/css';
2+
3+
export const Button = css({
4+
padding: '10px 24px',
5+
background: 'linear-gradient(90deg, #6a11cb 0%, #2575fc 100%)',
6+
color: 'white',
7+
border: 'none',
8+
borderRadius: '8px',
9+
fontWeight: 'bold',
10+
fontSize: '16px',
11+
cursor: 'pointer',
12+
boxShadow: '0 2px 8px rgba(106, 17, 203, 0.15)',
13+
transition: 'background 0.2s, transform 0.1s',
14+
_hover: {
15+
background: 'linear-gradient(90deg, #2575fc 0%, #6a11cb 100%)',
16+
transform: 'translateY(-2px) scale(1.03)',
17+
},
18+
_active: {
19+
background: 'linear-gradient(90deg, #6a11cb 0%, #2575fc 100%)',
20+
transform: 'scale(0.98)',
21+
},
22+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import client from '@/common/utils/client';
2+
import { useQuery } from '@tanstack/react-query';
3+
4+
const getIsLogin = async () => {
5+
const response = await client.get('/api/test/is-login');
6+
return response.data;
7+
};
8+
9+
const useGetIsLogin = () => {
10+
return useQuery({
11+
queryKey: ['isLogin'],
12+
queryFn: getIsLogin,
13+
enabled: false,
14+
});
15+
};
16+
17+
export default useGetIsLogin;

src/common/utils/client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import axios, { AxiosError } from 'axios';
2+
3+
const client = axios.create({
4+
baseURL: import.meta.env.VITE_API_URL,
5+
headers: {
6+
'Content-Type': 'application/json',
7+
},
8+
withCredentials: true,
9+
});
10+
11+
const REFRESH_URL = '/api/v1/refresh-token';
12+
13+
// TODO: Pending Requests Queue 추가 필요
14+
// 여러 요청이 동시에 발생할 경우 문제 발생 가능
15+
const responseErrorHandler = async (error: AxiosError) => {
16+
const { config, response } = error;
17+
18+
if (response?.status === 401 && config && config.url !== REFRESH_URL) {
19+
try {
20+
await client.post(REFRESH_URL);
21+
return client(config);
22+
} catch {
23+
return Promise.reject(error);
24+
}
25+
}
26+
27+
return Promise.reject(error);
28+
};
29+
30+
client.interceptors.response.use(response => response, responseErrorHandler);
31+
export default client;

src/pages/HomePage/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ActivityComponentType } from '@stackflow/react';
55
import * as s from './style.css';
66

77
import AppScreenWithSafeArea from '@/common/components/AppScreenWithSafeArea';
8+
import TestAPIButton from '@/common/components/TestAPIButton';
89

910
const HomePage: ActivityComponentType = () => {
1011
const { push } = useFlow();
@@ -16,6 +17,7 @@ const HomePage: ActivityComponentType = () => {
1617
<a href={`${import.meta.env.VITE_API_URL}/oauth2/authorization/kakao`}>카카오톡 로그인</a>
1718
<a href={`${import.meta.env.VITE_API_URL}/oauth2/authorization/google`}>구글 로그인</a>
1819
<i className={s.Container}>백엔드에몽 로그인 고쳐줘</i>
20+
<TestAPIButton />
1921
<button onClick={() => push('PostPage', {})}>이동</button>
2022
</div>
2123
</AppScreenWithSafeArea>

0 commit comments

Comments
 (0)