Skip to content

Commit efe168e

Browse files
authored
[FE] fix: 로그인 성공 후 헤더가 바뀌지 않는 오류 해결 (#428)
* fix: 로그인시 헤더가 바뀌지 않는 문제 해결 * refactor: eslint 적용
1 parent 241a9a4 commit efe168e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

frontend/src/layout/header/Header.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useContext, useState } from 'react';
2-
import { Link, useNavigate } from 'react-router-dom';
2+
import { Link, useLocation, useNavigate } from 'react-router-dom';
33

44
import { PATH } from '@constants';
55

@@ -27,6 +27,8 @@ const Header: React.FC = () => {
2727
const [isOpenDropDownBox, setIsOpenDropDownBox] = useState(false);
2828

2929
const navigate = useNavigate();
30+
const location = useLocation();
31+
3032
const { logout, isLoggedIn } = useAuth();
3133
const { userInfo } = useUserInfo();
3234

@@ -41,6 +43,10 @@ const Header: React.FC = () => {
4143
navigate(PATH.MAIN);
4244
};
4345

46+
const handleLoginButtonClick = () => {
47+
window.sessionStorage.setItem('prevPath', location.pathname);
48+
};
49+
4450
const handleLogoutButtonClick = () => {
4551
logout();
4652
};
@@ -94,7 +100,7 @@ const Header: React.FC = () => {
94100
</nav>
95101
) : (
96102
<a href={`https://github.com/login/oauth/authorize?client_id=${process.env.CLIENT_ID}`}>
97-
<NavButton ariaLabel="로그인">
103+
<NavButton ariaLabel="로그인" onClick={handleLoginButtonClick}>
98104
<LoginIcon />
99105
<span>Github 로그인</span>
100106
</NavButton>

frontend/src/pages/login-redirect-page/hooks/useLoginRedirectPage.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useAuth } from '@hooks/useAuth';
1010
const useLoginRedirectPage = () => {
1111
const [searchParams] = useSearchParams();
1212
const codeParam = searchParams.get('code');
13+
1314
const navigate = useNavigate();
1415

1516
const { login } = useAuth();
@@ -23,16 +24,19 @@ const useLoginRedirectPage = () => {
2324
return;
2425
}
2526

27+
// 외부 사이트에서 리다이렉트하는 것이기 때문에 react-router의 location state를 사용할 수 없어 sessionStorage를 이용
28+
const prevPath = window.sessionStorage.getItem('prevPath') || PATH.MAIN;
29+
2630
mutate(
2731
{ code: codeParam },
2832
{
2933
onError: () => {
3034
alert('로그인에 실패했습니다.');
31-
navigate(PATH.MAIN, { replace: true });
35+
navigate(prevPath, { replace: true });
3236
},
3337
onSuccess: ({ accessToken, expiredTime }) => {
3438
login(accessToken, expiredTime);
35-
navigate(-1);
39+
navigate(prevPath, { replace: true });
3640
},
3741
},
3842
);

0 commit comments

Comments
 (0)