diff --git a/src/apis/index.ts b/src/apis/index.ts index a19e397a..b1786b61 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -67,3 +67,10 @@ httpClient.interceptors.response.use( ); export default httpClient; + +export const logout = () => { + localStorage.removeItem('accessToken'); + localStorage.removeItem('refreshToken'); + + window.location.href = '/auth/SignIn'; +}; diff --git a/src/apis/postGoogleOauth.ts b/src/apis/postGoogleOauth.ts index 3ae9c7e6..61aaad3f 100644 --- a/src/apis/postGoogleOauth.ts +++ b/src/apis/postGoogleOauth.ts @@ -5,9 +5,9 @@ import httpClient from '.'; const getGoogleIdToken = async (code: string) => { const response = await axios.post('https://oauth2.googleapis.com/token', { code, - client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID_TEST, - client_secret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET_TEST, - redirect_uri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI_TEST, + client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID, + client_secret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET, + redirect_uri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI, grant_type: 'authorization_code', }); @@ -19,7 +19,7 @@ const postGoogleOauth = async (code: string) => { const idToken = tokenResponse.id_token; const response = await httpClient.post('/auth/signIn/GOOGLE', { - redirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI_TEST, + redirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI, token: idToken, }); diff --git a/src/pages/auth/SignIn.tsx b/src/pages/auth/SignIn.tsx index 3b80eca1..7c297ab6 100644 --- a/src/pages/auth/SignIn.tsx +++ b/src/pages/auth/SignIn.tsx @@ -1,3 +1,5 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; import Image from 'next/image'; import Link from 'next/link'; import { Input } from '@/components/ui/input'; @@ -7,12 +9,18 @@ import { zodResolver } from '@hookform/resolvers/zod'; import { Form, FormControl, FormField, FormItem, FormMessage } from '@/components/ui/form'; import { PostSigninRequest, PostSigninRequestType } from '@/schema/auth'; import useSigninMutation from '@/hooks/useSignInMutation'; -import { useRouter } from 'next/router'; export default function SignIn() { const mutationSignin = useSigninMutation(); const router = useRouter(); + useEffect(() => { + const accessToken = localStorage.getItem('accessToken'); + if (accessToken) { + router.push('/epigrams'); + } + }, [router]); + // 폼 정의 const form = useForm({ resolver: zodResolver(PostSigninRequest), @@ -27,19 +35,6 @@ export default function SignIn() { form.setValue(fieldName, value.trim(), { shouldValidate: true, shouldDirty: true }); }; - const handleSubmit = async (values: PostSigninRequestType) => { - try { - const result = await mutationSignin.mutateAsync(values); - localStorage.setItem('accessToken', result.accessToken); - localStorage.setItem('refreshToken', result.refreshToken); - router.push('/epigrams'); // 로그인 성공 후 이동할 페이지 - } catch (error) { - // 로그인 실패 처리 - /* eslint-disable no-console */ - console.error('로그인 실패:', error); - } - }; - return (
@@ -48,7 +43,7 @@ export default function SignIn() {
- + mutationSignin.mutate(values))} className='flex flex-col items-center lg:gap-6 gap-5 w-full px-6'>
logo-google diff --git a/src/pages/auth/SignUp.tsx b/src/pages/auth/SignUp.tsx index 3c989445..bd21425c 100644 --- a/src/pages/auth/SignUp.tsx +++ b/src/pages/auth/SignUp.tsx @@ -1,4 +1,5 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; +import { useRouter } from 'next/router'; import Image from 'next/image'; import Link from 'next/link'; import { zodResolver } from '@hookform/resolvers/zod'; @@ -11,6 +12,14 @@ import useRegisterMutation from '@/hooks/useRegisterMutation'; export default function SignUp() { const [focusedField, setFocusedField] = useState(null); + const router = useRouter(); + + useEffect(() => { + const accessToken = localStorage.getItem('accessToken'); + if (accessToken) { + router.push('/epigrams'); + } + }, [router]); const form = useForm({ resolver: zodResolver(PostSignUpRequest), diff --git a/src/pages/auth/callback/google/index.ts b/src/pages/auth/callback/google/index.ts index 8b9275e7..320477e4 100644 --- a/src/pages/auth/callback/google/index.ts +++ b/src/pages/auth/callback/google/index.ts @@ -1,11 +1,11 @@ import { useEffect } from 'react'; import { useSearchParams } from 'next/navigation'; -import useGoogleLogin from '@/hooks/useGoogleLogin'; // useGoogleLogin 훅을 가져옵니다 +import useGoogleLogin from '@/hooks/useGoogleLogin'; export default function Google() { const searchParams = useSearchParams(); - const code = searchParams.get('code'); // URL에서 'code' 값을 가져옵니다 - const { mutate: login } = useGoogleLogin(); // useGoogleLogin 훅에서 mutate 함수를 가져옵니다 + const code = searchParams.get('code'); + const { mutate: login } = useGoogleLogin(); useEffect(() => { if (code) { @@ -15,6 +15,4 @@ export default function Google() { console.log('No code found in URL parameters'); // code가 없을 때 콘솔에 출력 } }, [code, login]); - - return null; // 컴포넌트가 UI를 렌더링하지 않음 } diff --git a/src/pages/auth/redirect/google-callback/index.ts b/src/pages/auth/redirect/google-callback/index.ts deleted file mode 100644 index 1f925901..00000000 --- a/src/pages/auth/redirect/google-callback/index.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { useEffect } from 'react'; -import { useSearchParams } from 'next/navigation'; -import useGoogleLogin from '@/hooks/useGoogleLogin'; - -export default function Google() { - const searchParams = useSearchParams(); - const code = searchParams.get('code'); - const { mutate: login } = useGoogleLogin(); - - useEffect(() => { - if (code) { - login(code); - } else { - /* eslint-disable no-console */ - console.log(code); // code가 없을 때 콘솔에 출력 - } - }, [code, login]); -} - -// code가 없는 경우의 예시 http://localhost:3000/auth/redirect/kakao -// 토스트로 에러 메시지 띄우고, 로그인 페이지로 리다이렉트