diff --git a/src/components/button/KakaoLoginButton.tsx b/src/components/button/KakaoLoginButton.tsx index 426f22d..cec87b4 100644 --- a/src/components/button/KakaoLoginButton.tsx +++ b/src/components/button/KakaoLoginButton.tsx @@ -1,5 +1,8 @@ const KakaoLoginButton = () => { - const kakaoURL = `https://kauth.kakao.com/oauth/authorize?client_id=${import.meta.env.VITE_KAKAO_REST_API_KEY}&redirect_uri=${import.meta.env.VITE_KAKAO_REDIRECT_URI}&response_type=code`; + const kakaoRestApiKey = import.meta.env.VITE_KAKAO_REST_API_KEY; + const kakaoRedirectUrl = import.meta.env.VITE_KAKAO_REDIRECT_URI; + + const kakaoURL = `https://kauth.kakao.com/oauth/authorize?client_id=${kakaoRestApiKey}&redirect_uri=${kakaoRedirectUrl}&response_type=code`; const handleClick = () => { window.location.href = kakaoURL; @@ -9,7 +12,7 @@ const KakaoLoginButton = () => { diff --git a/src/pages/KaKaoLogin.tsx b/src/pages/KaKaoLogin.tsx index a23f734..ee1bfa6 100644 --- a/src/pages/KaKaoLogin.tsx +++ b/src/pages/KaKaoLogin.tsx @@ -11,10 +11,10 @@ const KaKaoLogin = () => { const getTokenAndLogin = async () => { if (typeof code === "string") { try { - await login(code); - navigate("/"); + const res = await login(code); + if (res.ok) navigate("/"); } catch (err) { - console.error("카카오 로그인에 실패했습니다."); + navigate("/error"); } } }; diff --git a/src/store/useAuthStore.ts b/src/store/useAuthStore.ts index a8ea6ae..92673b0 100644 --- a/src/store/useAuthStore.ts +++ b/src/store/useAuthStore.ts @@ -5,7 +5,7 @@ import instance from "@/api/axios"; interface AuthStore { isLoggedIn: boolean; accessToken: string | null; - login: (code: string) => Promise; + login: (code: string) => Promise<{ ok: boolean }>; logout: () => void; } @@ -16,15 +16,19 @@ const useAuthStore = create( accessToken: null, login: async (code: string) => { try { - const res = await instance.get(`/api/kakao/login?code=${code}`); - if (res.data) { - set({ - isLoggedIn: true, - accessToken: res.data as string - }); - } + const res = await instance.get( + `/api/kakao/login?code=${code}` + // + "&redirectUrl=https://localhost:5173/kakao-login" + ); + set({ + isLoggedIn: true, + accessToken: res.data as string + }); + return { + ok: true + }; } catch (error) { - console.error("Error during login:", error); + throw error; } }, logout: () => { @@ -32,6 +36,7 @@ const useAuthStore = create( isLoggedIn: false, accessToken: null }); + return { ok: true }; } }), {