Skip to content

Commit 67c952f

Browse files
authored
Merge pull request #172 from prgrms-web-devcourse-final-project/feat/169-kakao-login
[feat] 카카오 로그인 구현
2 parents b84c0de + e712648 commit 67c952f

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import TestLoginModal from '@/components/testLogin/TestLoginModal';
2626

2727
import { useSheetStore } from './store/sheetStore';
2828
import AnimatedLayout from '@/layouts/AnimatedLayout';
29+
import KaKaoRedirection from '@/components/KaKaoRedirection';
2930

3031
function App() {
3132
const location = useLocation();
@@ -83,6 +84,8 @@ function App() {
8384
<Route path="/mypage/blocklist" element={<BlockList />} />
8485
<Route path="/user/:userId" element={<UserProfile isMyPage={false} />} />
8586
</Route>
87+
88+
<Route path="/auth/login/kakao/callback" element={<KaKaoRedirection />} />
8689
<Route path="*" element={<NotFound />} />
8790
</Route>
8891
</Routes>

src/apis/auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ export const reissueToken = async () => {
4040
}
4141
return data;
4242
};
43+
44+
// 카카오 로그인
45+
export const getKakaoLogin = async (code: string) => {
46+
const { data } = await axiosInstance.get('/auth/login/kakao', { params: { code } });
47+
return data;
48+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getKakaoLogin } from '@/apis/auth';
2+
import Loading from '@/components/loading/Loading';
3+
import { useAuthStore } from '@/store/authStore';
4+
import { useEffect } from 'react';
5+
import { useNavigate } from 'react-router';
6+
7+
function KaKaoRedirection() {
8+
const navigate = useNavigate();
9+
const kakaoCode = new URL(window.location.href).searchParams.get('code');
10+
const { setAccessToken } = useAuthStore();
11+
12+
const handleKaKaoLogin = async () => {
13+
try {
14+
const { code, data } = await getKakaoLogin(kakaoCode as string);
15+
if (code === 200) {
16+
setAccessToken(data.accessToken);
17+
navigate('/home');
18+
} else {
19+
throw new Error('로그인 에러');
20+
}
21+
} catch (error) {
22+
console.error('로그인 에러가 발생했습니다.');
23+
}
24+
};
25+
26+
useEffect(() => {
27+
handleKaKaoLogin();
28+
}, []);
29+
return <Loading />;
30+
}
31+
32+
export default KaKaoRedirection;

src/pages/Login.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ export default function Login() {
7171
resetInputs();
7272
};
7373

74+
const handleKakaoLogin = () => {
75+
const KAKAO_CLIENT_ID = import.meta.env.VITE_KAKAO_CLIENT_ID;
76+
const KAKAO_REDIRECT_URI = import.meta.env.VITE_KAKAO_REDIRECT_URI;
77+
const link = `https://kauth.kakao.com/oauth/authorize?client_id=${KAKAO_CLIENT_ID}&redirect_uri=${KAKAO_REDIRECT_URI}&response_type=code`;
78+
window.location.href = link;
79+
};
80+
7481
const renderButtonContent = () => {
7582
if (isLoading) {
7683
return <SpinLoading />;
@@ -119,7 +126,10 @@ export default function Login() {
119126

120127
<div className="flex flex-col gap-3 items-center w-full mt-2.5 ">
121128
{/* 소셜 로그인 */}
122-
<Button className="bg-[#FEE500] text-gray-80 gap-1.5 hover:bg-[#fded63]">
129+
<Button
130+
className="bg-[#FEE500] text-gray-80 gap-1.5 hover:bg-[#fded63]"
131+
onClick={handleKakaoLogin}
132+
>
123133
<img className="w-4" src={kakao} alt="카카오 아이콘" />
124134
<span className="text-black/85">카카오로 시작하기</span>
125135
</Button>

0 commit comments

Comments
 (0)