|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { useNavigate, useSearchParams } from 'react-router-dom'; |
| 3 | + |
| 4 | +import { useGetAuthTicket } from '@/entities/auth'; |
| 5 | + |
| 6 | +import { ROUTER_PATH } from '@/shared'; |
| 7 | + |
| 8 | +export default function OAuthRedirectPage() { |
| 9 | + const [searchParams] = useSearchParams(); |
| 10 | + const navigate = useNavigate(); |
| 11 | + const [error, setError] = useState<string | null>(null); |
| 12 | + |
| 13 | + // URL에서 ticket 파라미터 추출 |
| 14 | + const ticket = searchParams.get('ticket'); |
| 15 | + const oauthError = searchParams.get('error'); |
| 16 | + const errorDescription = searchParams.get('error_description'); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + if (oauthError) { |
| 20 | + setError(errorDescription || `OAuth 인증 오류: ${oauthError}`); |
| 21 | + } else if (!ticket) { |
| 22 | + setError('인증 토큰이 없습니다. 다시 로그인해주세요.'); |
| 23 | + } |
| 24 | + }, [oauthError, errorDescription, ticket]); |
| 25 | + |
| 26 | + // ticket이 있고 OAuth 에러가 없을 때만 API 호출 |
| 27 | + const shouldCallApi = !!ticket && !oauthError; |
| 28 | + const { |
| 29 | + data: authData, |
| 30 | + error: apiError, |
| 31 | + isLoading, |
| 32 | + } = useGetAuthTicket(shouldCallApi ? ticket : ''); |
| 33 | + |
| 34 | + // API 에러 처리 |
| 35 | + useEffect(() => { |
| 36 | + if (apiError) { |
| 37 | + console.error('API 호출 중 오류:', apiError); |
| 38 | + setError('인증 검증에 실패했습니다. 다시 시도해주세요.'); |
| 39 | + } |
| 40 | + }, [apiError]); |
| 41 | + |
| 42 | + // 성공 시 처리 |
| 43 | + useEffect(() => { |
| 44 | + if (authData && !isLoading && !apiError && shouldCallApi) { |
| 45 | + console.log('OAuth 인증 성공!', authData); |
| 46 | + |
| 47 | + // refreshToken을 localStorage에 저장 |
| 48 | + localStorage.setItem('refreshToken', authData.refreshToken); |
| 49 | + |
| 50 | + // 메인 페이지로 이동 |
| 51 | + navigate(ROUTER_PATH.MAIN, { replace: true }); |
| 52 | + } |
| 53 | + }, [authData, isLoading, apiError, navigate, shouldCallApi]); |
| 54 | + |
| 55 | + if (isLoading && shouldCallApi) { |
| 56 | + return ( |
| 57 | + <div className='flex min-h-screen items-center justify-center'> |
| 58 | + <div className='text-center'> |
| 59 | + <div className='mb-4'> |
| 60 | + <div className='mx-auto h-8 w-8 animate-spin rounded-full border-4 border-blue-500 border-t-transparent'></div> |
| 61 | + </div> |
| 62 | + <p className='text-lg font-medium text-gray-700'>인증 처리 중...</p> |
| 63 | + <p className='mt-2 text-sm text-gray-500'>잠시만 기다려주세요.</p> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (error) { |
| 70 | + return ( |
| 71 | + <div className='flex min-h-screen items-center justify-center'> |
| 72 | + <div className='text-center'> |
| 73 | + <div className='mb-4'> |
| 74 | + <div className='mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-red-100'> |
| 75 | + <svg |
| 76 | + className='h-6 w-6 text-red-600' |
| 77 | + fill='none' |
| 78 | + viewBox='0 0 24 24' |
| 79 | + stroke='currentColor' |
| 80 | + > |
| 81 | + <path |
| 82 | + strokeLinecap='round' |
| 83 | + strokeLinejoin='round' |
| 84 | + strokeWidth={2} |
| 85 | + d='M6 18L18 6M6 6l12 12' |
| 86 | + /> |
| 87 | + </svg> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + <h2 className='mb-2 text-xl font-semibold text-gray-900'>인증 실패</h2> |
| 91 | + <p className='mb-4 text-gray-600'>{error}</p> |
| 92 | + <button |
| 93 | + onClick={() => navigate(ROUTER_PATH.LOGIN)} |
| 94 | + className='rounded-lg bg-blue-500 px-4 py-2 text-white transition-colors hover:bg-blue-600' |
| 95 | + > |
| 96 | + 로그인 페이지로 돌아가기 |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return null; |
| 104 | +} |
0 commit comments