|
1 | 1 | import { useEffect } from "react"; |
2 | 2 | import { useNavigate, useLocation } from "react-router-dom"; |
3 | 3 | import { useFetchUser } from "../../hooks/auth/useFetchUser"; |
4 | | -import { User } from "../../store/auth"; |
5 | 4 |
|
6 | 5 | const useRootRedirect = () => { |
7 | 6 | const navigate = useNavigate(); |
8 | 7 | const location = useLocation(); |
9 | 8 | const accessToken = localStorage.getItem("access_token"); |
10 | 9 | const refreshToken = localStorage.getItem("refresh_token"); |
11 | 10 |
|
12 | | - const result = useFetchUser(accessToken, refreshToken, undefined); |
13 | | - const user = result.user as User | null; |
| 11 | + const { user, loading } = useFetchUser(accessToken, refreshToken, undefined); |
14 | 12 |
|
15 | 13 | useEffect(() => { |
16 | | - if (!user) return; |
| 14 | + if (loading || !user) return; |
17 | 15 |
|
18 | 16 | const hasPrivacy = !!user.privacy; |
19 | 17 |
|
| 18 | + const hasConcerns = !!user.addition?.concerns; |
| 19 | + const hasEmotions = !!user.addition?.emotions; |
| 20 | + |
20 | 21 | if ((!accessToken && !refreshToken) || !user.email || user.id == null) { |
21 | 22 | navigate("/login", { replace: true }); |
22 | 23 | return; |
23 | 24 | } |
24 | 25 |
|
25 | | - if (location.pathname.includes("/onboarding")) { |
26 | | - if (!hasPrivacy) { |
27 | | - navigate("/onboarding/basic", { replace: true }); |
28 | | - } else { |
29 | | - navigate("/home", { replace: true }); |
30 | | - } |
31 | | - return; |
32 | | - } |
33 | | - |
34 | 26 | if ( |
35 | 27 | location.pathname === "" || |
36 | 28 | location.pathname === "/" || |
37 | 29 | location.pathname === "/home" |
38 | 30 | ) { |
39 | 31 | if (!hasPrivacy) { |
40 | 32 | navigate("/onboarding/basic", { replace: true }); |
| 33 | + } else if (!hasConcerns || !hasEmotions) { |
| 34 | + navigate("/onboarding/additional/concern", { replace: true }); |
41 | 35 | } |
42 | 36 | return; |
43 | 37 | } |
44 | | - }, [navigate, location, accessToken, refreshToken, user]); |
| 38 | + }, [navigate, location, accessToken, refreshToken, user, loading]); |
45 | 39 |
|
46 | 40 | return null; |
47 | 41 | }; |
|
0 commit comments