File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed
pages/login-redirect-page/hooks Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change 1
1
import { useContext , useState } from 'react' ;
2
- import { Link , useNavigate } from 'react-router-dom' ;
2
+ import { Link , useLocation , useNavigate } from 'react-router-dom' ;
3
3
4
4
import { PATH } from '@constants' ;
5
5
@@ -27,6 +27,8 @@ const Header: React.FC = () => {
27
27
const [ isOpenDropDownBox , setIsOpenDropDownBox ] = useState ( false ) ;
28
28
29
29
const navigate = useNavigate ( ) ;
30
+ const location = useLocation ( ) ;
31
+
30
32
const { logout, isLoggedIn } = useAuth ( ) ;
31
33
const { userInfo } = useUserInfo ( ) ;
32
34
@@ -41,6 +43,10 @@ const Header: React.FC = () => {
41
43
navigate ( PATH . MAIN ) ;
42
44
} ;
43
45
46
+ const handleLoginButtonClick = ( ) => {
47
+ window . sessionStorage . setItem ( 'prevPath' , location . pathname ) ;
48
+ } ;
49
+
44
50
const handleLogoutButtonClick = ( ) => {
45
51
logout ( ) ;
46
52
} ;
@@ -94,7 +100,7 @@ const Header: React.FC = () => {
94
100
</ nav >
95
101
) : (
96
102
< a href = { `https://github.com/login/oauth/authorize?client_id=${ process . env . CLIENT_ID } ` } >
97
- < NavButton ariaLabel = "로그인" >
103
+ < NavButton ariaLabel = "로그인" onClick = { handleLoginButtonClick } >
98
104
< LoginIcon />
99
105
< span > Github 로그인</ span >
100
106
</ NavButton >
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { useAuth } from '@hooks/useAuth';
10
10
const useLoginRedirectPage = ( ) => {
11
11
const [ searchParams ] = useSearchParams ( ) ;
12
12
const codeParam = searchParams . get ( 'code' ) ;
13
+
13
14
const navigate = useNavigate ( ) ;
14
15
15
16
const { login } = useAuth ( ) ;
@@ -23,16 +24,19 @@ const useLoginRedirectPage = () => {
23
24
return ;
24
25
}
25
26
27
+ // 외부 사이트에서 리다이렉트하는 것이기 때문에 react-router의 location state를 사용할 수 없어 sessionStorage를 이용
28
+ const prevPath = window . sessionStorage . getItem ( 'prevPath' ) || PATH . MAIN ;
29
+
26
30
mutate (
27
31
{ code : codeParam } ,
28
32
{
29
33
onError : ( ) => {
30
34
alert ( '로그인에 실패했습니다.' ) ;
31
- navigate ( PATH . MAIN , { replace : true } ) ;
35
+ navigate ( prevPath , { replace : true } ) ;
32
36
} ,
33
37
onSuccess : ( { accessToken, expiredTime } ) => {
34
38
login ( accessToken , expiredTime ) ;
35
- navigate ( - 1 ) ;
39
+ navigate ( prevPath , { replace : true } ) ;
36
40
} ,
37
41
} ,
38
42
) ;
You can’t perform that action at this time.
0 commit comments