diff --git a/src/apis/oauth.ts b/src/apis/oauth.ts deleted file mode 100644 index ae6bd068..00000000 --- a/src/apis/oauth.ts +++ /dev/null @@ -1,11 +0,0 @@ -import axios from 'axios'; - -const postOauth = async (code: string) => { - const response = await axios.post(`${process.env.NEXT_PUBLIC_BASE_URL}/auth/signIn/KAKAO`, { - redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI, - token: code, - }); - return response.data; -}; - -export default postOauth; diff --git a/src/apis/postKakaoOauth.ts b/src/apis/postKakaoOauth.ts new file mode 100644 index 00000000..c85c2eda --- /dev/null +++ b/src/apis/postKakaoOauth.ts @@ -0,0 +1,11 @@ +import httpClient from '.'; + +const postKakaoOauth = async (code: string) => { + const response = await httpClient.post('/auth/signIn/KAKAO', { + redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI, + token: code, + }); + return response.data; +}; + +export default postKakaoOauth; diff --git a/src/hooks/useKakaoLogin.ts b/src/hooks/useKakaoLogin.ts index ea627868..1652fd29 100644 --- a/src/hooks/useKakaoLogin.ts +++ b/src/hooks/useKakaoLogin.ts @@ -1,4 +1,4 @@ -import postOauth from '@/apis/oauth'; +import postKakaoOauth from '@/apis/postKakaoOauth'; import { toast } from '@/components/ui/use-toast'; import { useMutation } from '@tanstack/react-query'; import { isAxiosError } from 'axios'; @@ -9,7 +9,7 @@ const useKakaoLogin = () => { return useMutation({ mutationFn: async (code: string) => { - const result = await postOauth(code); + const result = await postKakaoOauth(code); localStorage.setItem('accessToken', result.accessToken); localStorage.setItem('refreshToken', result.refreshToken); return result; diff --git a/src/hooks/useRegisterMutation.ts b/src/hooks/useRegisterMutation.ts index a0e8e8cb..67e7661c 100644 --- a/src/hooks/useRegisterMutation.ts +++ b/src/hooks/useRegisterMutation.ts @@ -12,10 +12,9 @@ const useRegisterMutation = (onRegisterError: (field: 'email' | 'nickname') => v onSuccess: (data) => { localStorage.setItem('accessToken', data.accessToken); localStorage.setItem('refreshToken', data.refreshToken); - router.push('/auth/SignIn'); + router.push('/epigrams'); toast({ title: '회원가입 성공!', - description: '로그인 후 이용해주세요.', className: 'bg-illust-green text-white font-semibold', }); }, diff --git a/src/pages/auth/SignIn.tsx b/src/pages/auth/SignIn.tsx index 7e4af11c..abb180c7 100644 --- a/src/pages/auth/SignIn.tsx +++ b/src/pages/auth/SignIn.tsx @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken'; export default function SignIn() { const mutationSignin = useSigninMutation(); - const { mutate: refreshAccessToken } = useRefreshToken(); + const { mutateAsync: refreshAccessToken } = useRefreshToken(); const router = useRouter(); useEffect(() => { const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null; if (refreshToken) { - refreshAccessToken({ refreshToken }); - router.push('/epigrams'); + const handleTokenRefresh = async () => { + try { + await refreshAccessToken({ refreshToken }); + router.push('/epigrams'); // 토큰 갱신 후 리디렉션 + } catch { + localStorage.removeItem('refreshToken'); + router.push('/auth/SignIn'); + } + }; + + handleTokenRefresh(); } - }, [refreshAccessToken]); + }, [refreshAccessToken, router]); // 폼 정의 const form = useForm({ diff --git a/src/pages/auth/SignUp.tsx b/src/pages/auth/SignUp.tsx index 37a829fa..edad378f 100644 --- a/src/pages/auth/SignUp.tsx +++ b/src/pages/auth/SignUp.tsx @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken'; export default function SignUp() { const [focusedField, setFocusedField] = useState(null); - const { mutate: refreshAccessToken } = useRefreshToken(); + const { mutateAsync: refreshAccessToken } = useRefreshToken(); const router = useRouter(); useEffect(() => { const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null; if (refreshToken) { - refreshAccessToken({ refreshToken }); - router.push('/epigrams'); + const handleTokenRefresh = async () => { + try { + await refreshAccessToken({ refreshToken }); + router.push('/epigrams'); // 토큰 갱신 후 리디렉션 + } catch { + localStorage.removeItem('refreshToken'); + router.push('/auth/SignIn'); + } + }; + + handleTokenRefresh(); } - }, [refreshAccessToken]); + }, [refreshAccessToken, router]); const form = useForm({ resolver: zodResolver(PostSignUpRequest),