Skip to content

Commit

Permalink
feat : kakaoLogin api 모킹 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
hyesung99 committed Oct 25, 2023
1 parent 923977f commit 71977be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/mocks/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { END_POINTS } from '@/constants/api';
import { HttpResponse, http } from 'msw';

import { END_POINTS } from '@constants/api';

interface member {
name: string;
number: string;
Expand All @@ -26,4 +27,11 @@ export const userHandlers = [

return HttpResponse.json({ status: 201 });
}),

http.post(END_POINTS.KAKAO_LOGIN, async () => {
return HttpResponse.json({
token: 'test token',
isNewUser: true,
});
}),
];
36 changes: 26 additions & 10 deletions src/pages/OauthRedirectPage.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { kakaoLogin } from '@/apis/auth/kakaoLogin';
import { PATH } from '@/constants/path';
import { setStorage } from '@/utils/localStorage';

import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

const OauthRedirectPage = () => {
const authCode = new URL(window.location.href).searchParams.get('code');
const navigate = useNavigate();

useEffect(() => {
if (!authCode) {
throw new Error('인증 코드가 없습니다.');
}

kakaoLogin(authCode).then(({ accessToken }) => {
localStorage.setItem('accessToken', accessToken);
window.location.href = '/';
});
});
const requestKakaoLogin = async () => {
try {
const authCode = new URL(window.location.href).searchParams.get('code');
if (!authCode) {
throw new Error('인증 코드가 없습니다.');
}

const { token, isNewUser } = await kakaoLogin(authCode);
setStorage('token', token);

if (isNewUser) {
navigate(PATH.REGISTER);
} else {
navigate(PATH.MAIN);
}
} catch (error) {
//TODO: 에러 처리 navigate('/error');
}
};

requestKakaoLogin();
}, [navigate]);

return <div>OauthRedirectPage</div>;
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface User {
accessToken: string;
token: string;
}

interface AvatarShapeType {
Expand Down

0 comments on commit 71977be

Please sign in to comment.