Skip to content

Commit

Permalink
feat:#8 로그인 콜백 페이지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dalzzy committed Jan 7, 2025
1 parent 8a6c077 commit 845f02b
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/pages/SignUpPage/LoginCallback.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios from 'axios';
import { jwtDecode } from 'jwt-decode';
import { useEffect } from 'react';
import { useCookies } from 'react-cookie';
import { useNavigate } from 'react-router-dom';
Expand All @@ -12,7 +14,56 @@ const LoginCallback: React.FC = () => {
const [_, setCookie] = useCookies(['accessToken', 'refreshToken']);
const nav = useNavigate();

useEffect;
useEffect(() => {
const handleLoginResponse = async () => {
try {
const res = await axios.get(window.location.href, {
withCredentials: true,
});

const accessToken = res.headers['authorization']?.replace(
'Bearer ',
''
);
const refreshToken = res.headers['authorization-refresh']?.replace(
'Bearer ',
''
);

if (!accessToken) {
console.error('Access Token이 없습니다.');
nav('/intro');
return;
}

const decoded = jwtDecode<TokenPayload>(accessToken);
console.log('디코딩된 JWT : ', decoded);

setCookie('accessToken', accessToken, { path: '/', maxAge: 15 * 60 });
if (refreshToken) {
setCookie('refreshToken', refreshToken, {
path: '/',
maxAge: 7 * 24 * 60 * 60,
});
}

if (decoded.role === 'USER') {
nav('/home');
} else if (decoded.role === 'GUEST') {
nav('/signup');
} else {
console.error('알수 없는 사용자 role: ', decoded.role);
nav('/intro');
}
} catch (error) {
console.error('로그인 처리 중 오류가 발생했습니다. ', error);
}
};

handleLoginResponse();
}, [setCookie, nav]);

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

export default LoginCallback;

0 comments on commit 845f02b

Please sign in to comment.