Skip to content

Commit

Permalink
Merge pull request #115 from sw326/main
Browse files Browse the repository at this point in the history
fix double rendering
  • Loading branch information
sw326 authored Sep 11, 2024
2 parents f9ec5e7 + 81e0376 commit b11e4a0
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions src/pages/members/Redirection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, useCallback } from 'react';
import React, { useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../../hooks/useAuth';
import { useKakaoLogin } from '../../hooks/useMembers';
Expand All @@ -8,64 +8,46 @@ import {
} from '../../utils/errorHandler';

const Redirection: React.FC = () => {
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isLoginAttempted, setIsLoginAttempted] = useState(false);
const navigate = useNavigate();
const { login, isAuthenticated } = useAuth();
const kakaoLoginMutation = useKakaoLogin();
const isLoginAttemptedRef = useRef(false);

const performKakaoLogin = useCallback(
async (code: string) => {
if (isLoginAttempted) return;
setIsLoginAttempted(true);
useEffect(() => {
const performKakaoLogin = async (code: string) => {
if (isLoginAttemptedRef.current) return;
isLoginAttemptedRef.current = true;

try {
const { token, refreshToken } =
await kakaoLoginMutation.mutateAsync(code);

localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
login(token, refreshToken, false);

console.log('Login successful:', { token, refreshToken });
} catch (error) {
console.error('Login error:', error);
const errorMessage = handleApiError(error);
showErrorNotification(errorMessage);
setError(`로그인 처리 중 오류가 발생했습니다: ${errorMessage}`);
} finally {
setIsLoading(false);
navigate('/login', { state: { error: errorMessage } });
}
},
[kakaoLoginMutation, login, isLoginAttempted],
);
};

useEffect(() => {
const code = new URL(window.location.href).searchParams.get('code');
if (code && !isAuthenticated && !isLoginAttempted) {
if (code && !isAuthenticated) {
performKakaoLogin(code);
} else if (!code) {
setError('카카오 인증 코드가 없습니다.');
setIsLoading(false);
navigate('/login', { state: { error: '카카오 인증 코드가 없습니다.' } });
}
}, [isAuthenticated, performKakaoLogin, isLoginAttempted]);
}, [kakaoLoginMutation, login, isAuthenticated, navigate]);

useEffect(() => {
if (isAuthenticated) {
navigate('/memberhome');
}
}, [isAuthenticated, navigate]);

if (isLoading) {
return <div>로그인 중...</div>;
}

if (error) {
return <div>{error}</div>;
}

return null;
return <div>로그인 처리 중...</div>;
};

export default Redirection;

0 comments on commit b11e4a0

Please sign in to comment.