|
1 | 1 | import { useEffect, useState } from "react"; |
2 | | -import { Link, useLocation } from "react-router-dom"; |
| 2 | +import { Link } from "react-router-dom"; |
3 | 3 |
|
4 | 4 | export default function Home() { |
5 | 5 | const [darkMode, setDarkMode] = useState(() => { |
6 | 6 | const stored = localStorage.getItem("darkMode"); |
7 | 7 | return stored ? JSON.parse(stored) : false; |
8 | 8 | }); |
9 | 9 |
|
10 | | - const location = useLocation(); |
| 10 | + const [user, setUser] = useState<{ username: string } | null>(null); |
11 | 11 |
|
| 12 | + // Проверка токена |
12 | 13 | useEffect(() => { |
13 | | - if (darkMode) { |
14 | | - document.documentElement.classList.add("dark"); |
15 | | - } else { |
16 | | - document.documentElement.classList.remove("dark"); |
17 | | - } |
| 14 | + const token = localStorage.getItem("token"); |
| 15 | + if (!token) return; |
| 16 | + |
| 17 | + fetch("/api/auth/me", { |
| 18 | + headers: { Authorization: `Bearer ${token}` }, |
| 19 | + }) |
| 20 | + .then(async (res) => { |
| 21 | + if (res.ok) { |
| 22 | + const data = await res.json(); |
| 23 | + setUser(data); |
| 24 | + } else { |
| 25 | + setUser(null); |
| 26 | + } |
| 27 | + }) |
| 28 | + .catch(() => setUser(null)); |
| 29 | + }, []); |
| 30 | + |
| 31 | + // Dark mode toggle |
| 32 | + useEffect(() => { |
| 33 | + if (darkMode) document.documentElement.classList.add("dark"); |
| 34 | + else document.documentElement.classList.remove("dark"); |
18 | 35 | localStorage.setItem("darkMode", JSON.stringify(darkMode)); |
19 | 36 | }, [darkMode]); |
20 | 37 |
|
@@ -73,21 +90,40 @@ export default function Home() { |
73 | 90 | )} |
74 | 91 | </button> |
75 | 92 |
|
76 | | - <Link to="/login" className="login-button"> |
77 | | - <svg |
78 | | - viewBox="0 0 24 24" |
79 | | - xmlns="http://www.w3.org/2000/svg" |
80 | | - className="button-icon" |
81 | | - > |
82 | | - <path |
83 | | - fill="currentColor" |
84 | | - fillRule="evenodd" |
85 | | - d="M7.5 6.75a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM3.751 20.25a8.25 8.25 0 0116.498 0 .75.75 0 01-.75.75h-15a.75.75 0 01-.75-.75z" |
86 | | - clipRule="evenodd" |
87 | | - /> |
88 | | - </svg> |
89 | | - <span>Login</span> |
90 | | - </Link> |
| 93 | + {/* Авторизация */} |
| 94 | + {user ? ( |
| 95 | + <Link to="/dashboard" className="login-button"> |
| 96 | + <svg |
| 97 | + viewBox="0 0 24 24" |
| 98 | + xmlns="http://www.w3.org/2000/svg" |
| 99 | + className="button-icon" |
| 100 | + > |
| 101 | + <path |
| 102 | + fill="currentColor" |
| 103 | + fillRule="evenodd" |
| 104 | + d="M7.5 6.75a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM3.751 20.25a8.25 8.25 0 0116.498 0 .75.75 0 01-.75.75h-15a.75.75 0 01-.75-.75z" |
| 105 | + clipRule="evenodd" |
| 106 | + /> |
| 107 | + </svg> |
| 108 | + <span>{user.username}</span> |
| 109 | + </Link> |
| 110 | + ) : ( |
| 111 | + <Link to="/login" className="login-button"> |
| 112 | + <svg |
| 113 | + viewBox="0 0 24 24" |
| 114 | + xmlns="http://www.w3.org/2000/svg" |
| 115 | + className="button-icon" |
| 116 | + > |
| 117 | + <path |
| 118 | + fill="currentColor" |
| 119 | + fillRule="evenodd" |
| 120 | + d="M7.5 6.75a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM3.751 20.25a8.25 8.25 0 0116.498 0 .75.75 0 01-.75.75h-15a.75.75 0 01-.75-.75z" |
| 121 | + clipRule="evenodd" |
| 122 | + /> |
| 123 | + </svg> |
| 124 | + <span>Login</span> |
| 125 | + </Link> |
| 126 | + )} |
91 | 127 | </div> |
92 | 128 | </div> |
93 | 129 | </nav> |
|
0 commit comments