Skip to content

Commit

Permalink
fix: 비동기 수정중
Browse files Browse the repository at this point in the history
  • Loading branch information
urjimyu committed May 27, 2024
1 parent 732b70d commit b6df790
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
14 changes: 4 additions & 10 deletions src/hooks/queries/usePostKakao.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ const usePostKakao = () => {
const res = await kakao.postKakaoLogin({ kakaoToken: accessToken, fcmToken: '0' });
const data: KakaoLoginResponseType = res.data;
console.log('data', data);
setKakaoResponse({
userId: data.data.userId,
accessToken: data.data.accessToken,
isAlreadyUser: data.data.isAlreadyUser,
refreshToken: data.data.refreshToken,
nickname: data.data.nickname,
});
console.log('응답이 왔나요?', kakaoResponse);
localStorage.setItem('ACCESS_TOKEN', data.data.accessToken);
localStorage.setItem('REFRESH_TOKEN', data.data.refreshToken);
setKakaoResponse(data.data);
// console.log('응답이 왔나요?', kakaoResponse);
// localStorage.setItem('ACCESS_TOKEN', data.data.accessToken);
// localStorage.setItem('REFRESH_TOKEN', data.data.refreshToken);
} catch (err) {
setKakaoError(err as AxiosError);
console.log('에러 발생', err);
Expand Down
48 changes: 22 additions & 26 deletions src/pages/KakaoLoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,36 @@ const KakaoLoginPage = () => {
// let code = new URL(window.location.href).searchParams.get('code');
// console.log('CODE', code);

const { kakaoResponse, kakaoError, kakaoLoading, postKakao } = usePostKakao();
const { kakaoResponse, kakaoLoading, postKakao } = usePostKakao();

const handlePostKakao = async (code: string) => {
const fetchLogin = async () => {
try {
await postKakao(code);
if (!kakaoError && !kakaoLoading && kakaoResponse) {
console.log('이동 전 콘솔 확인', kakaoResponse);
if (kakaoResponse.isAlreadyUser) {
navigate('/delete');
} else {
navigate('/unregistered');
}
const res = await useGetToken();
if (res) {
const token = res.data.access_token;

// localStorage.setItem('ACCESS_TOKEN', JSON.stringify(token));
// await handlePostKakao(token);
await postKakao(token);
}
} catch (error) {
console.log('ERROR', error);
navigate('/unregistered');
} catch (err) {
console.log(err);
}
};

useEffect(() => {
const fetchLogin = async () => {
try {
const res = await useGetToken();
if (res) {
const token = res.data.access_token;
// localStorage.setItem('ACCESS_TOKEN', JSON.stringify(token));
await handlePostKakao(token);
}
} catch (err) {
console.log(err);
}
};
fetchLogin();
}, []);
}, [fetchLogin]);

useEffect(() => {
if (!kakaoLoading && kakaoResponse) {
if (kakaoResponse.isAlreadyUser) {
navigate('/delete');
} else {
navigate('/unregistered');
}
}
}, [kakaoLoading, kakaoResponse, navigate]);

return <></>;
};
Expand Down

0 comments on commit b6df790

Please sign in to comment.