|
1 | | -import { useEffect, useReducer } from 'react'; |
| 1 | +import { useEffect } from 'react'; |
2 | 2 | import { useNavigate, useLocation } from 'react-router-dom'; |
3 | | -import useAuthStore from 'stores/useAuthStore'; |
4 | 3 |
|
5 | | -import Alert from 'components/Alert'; |
6 | 4 | import Loading from 'pages/Loading'; |
7 | 5 |
|
8 | | -import { getAuthorization } from 'services/user'; |
9 | | -import CustomError from 'utils/customError'; |
10 | | - |
11 | | -function isCustomError(error: unknown): error is CustomError { |
12 | | - return (error as CustomError).custom !== undefined; |
13 | | -} |
| 6 | +import { refreshToken } from 'services/user'; |
14 | 7 |
|
15 | 8 | const Auth = () => { |
16 | 9 | const location = useLocation(); |
17 | 10 | const navigate = useNavigate(); |
18 | | - const login = useAuthStore((state) => state.login); |
19 | | - const logout = useAuthStore((state) => state.logout); |
20 | | - const [isAlertActive, handleAlertActive] = useReducer((prev) => !prev, false); |
21 | | - const [isAccessTokenAlertActive, handleAccessTokenAlertActive] = useReducer((prev) => !prev, false); |
22 | | - const [isWithdrawnAlertActive, handleWithdrawnAlertActive] = useReducer((prev) => !prev, false); |
23 | | - const [isRefreshAlertActive, handleRefreshAlertActive] = useReducer((prev) => !prev, false); |
24 | | - const [isTermAlertActive, handleTermAlertActive] = useReducer((prev) => !prev, false); |
25 | 11 |
|
26 | 12 | useEffect(() => { |
27 | | - const checkAuthorization = async () => { |
28 | | - const queryParam = new URLSearchParams(location.search); |
29 | | - const accessToken = queryParam.get('token'); |
30 | | - |
31 | | - try { |
32 | | - if (accessToken) { |
33 | | - login(accessToken); |
34 | | - const authorization = await getAuthorization(); |
35 | | - |
36 | | - if (authorization === 'GUEST') navigate('/signup'); |
37 | | - else if (authorization === 'USER') navigate('/'); |
38 | | - else if (authorization === 'LIMITED_USER') navigate('/'); // TODO : 권한이 업데이트 됐을 경우 권한이 업데이트 됨! 페이지로 |
39 | | - } // |
40 | | - // 엑세스 토큰이 없으면 얼럿 수행 |
41 | | - else { |
42 | | - handleAccessTokenAlertActive(); |
43 | | - logout(); |
44 | | - |
45 | | - setTimeout(() => { |
46 | | - navigate('/login'); |
47 | | - }, 2500); |
48 | | - } |
49 | | - } catch (error) { |
50 | | - if (isCustomError(error) && error.custom) { |
51 | | - if (error.message === '탈퇴한 회원입니다. 회원가입을 다시 진행해주세요') handleWithdrawnAlertActive(); |
52 | | - else if (error.message === '로그인 기한이 만료되었어요. 다시 로그인 해주세요') handleRefreshAlertActive(); |
53 | | - else if (error.message === '약관에 동의하지 않았어요. 회원가입을 다시 진행해주세요') handleTermAlertActive(); |
54 | | - } // |
55 | | - else { |
56 | | - handleAlertActive(); |
57 | | - } |
58 | | - |
59 | | - logout(); |
60 | | - setTimeout(() => { |
61 | | - navigate('/login'); |
62 | | - }, 2500); |
| 13 | + const queryParam = new URLSearchParams(location.search); |
| 14 | + const userStatus = queryParam.get('status'); |
| 15 | + |
| 16 | + const handleAuth = async () => { |
| 17 | + // 기존 유저일 경우 refresh를 통해 엑세스 토큰 발급 |
| 18 | + if (userStatus === 'EXISTING_USER') { |
| 19 | + await refreshToken(); |
| 20 | + navigate('/'); |
| 21 | + return; |
63 | 22 | } |
64 | | - }; |
65 | 23 |
|
66 | | - checkAuthorization(); |
67 | | - }, [location.search, navigate, login, logout]); |
| 24 | + // 신규 유저일 경우 회원가입 로직 |
| 25 | + if (userStatus === 'SIGNUP_REQUIRED') navigate('/signup'); |
| 26 | + }; |
68 | 27 |
|
69 | | - return ( |
70 | | - <> |
71 | | - {isAlertActive && <Alert message="오류가 발생했어요 다시 시도해주세요" onClose={handleAlertActive} />} |
72 | | - {isAccessTokenAlertActive && ( |
73 | | - <Alert message="로그인 과정 중 오류가 발생했어요 다시 시도해주세요" onClose={handleAccessTokenAlertActive} /> |
74 | | - )} |
75 | | - {isWithdrawnAlertActive && ( |
76 | | - <Alert message="탈퇴한 회원입니다. 회원가입을 다시 진행해주세요" onClose={handleWithdrawnAlertActive} /> |
77 | | - )} |
78 | | - {isRefreshAlertActive && ( |
79 | | - <Alert message="로그인 기한이 만료되었어요. 다시 로그인 해주세요" onClose={handleRefreshAlertActive} /> |
80 | | - )} |
81 | | - {isTermAlertActive && ( |
82 | | - <Alert message="약관에 동의하지 않았어요. 회원가입을 다시 진행해주세요'" onClose={handleTermAlertActive} /> |
83 | | - )} |
| 28 | + handleAuth(); |
| 29 | + }); |
84 | 30 |
|
85 | | - <Loading /> |
86 | | - </> |
87 | | - ); |
| 31 | + return <Loading />; |
88 | 32 | }; |
89 | 33 |
|
90 | 34 | export default Auth; |
0 commit comments