diff --git a/backend/main.go b/backend/main.go index 9eb0704..23bc8cd 100644 --- a/backend/main.go +++ b/backend/main.go @@ -33,20 +33,23 @@ func main() { c.Next() }) - r.POST("/register", handlers.Register) - r.POST("/login", handlers.Login) - - protected := r.Group("/api") - protected.Use(middleware.AuthRequired()) + api := r.Group("/api") { - protected.GET("/parts/:id", handlers.GetPart) - protected.GET("/parts", handlers.GetAllParts) - protected.GET("/orders", handlers.GetsOrders) - protected.POST("/parts", handlers.CreatePart) - protected.POST("/parts/bulk", handlers.ImportParts) - protected.PATCH("/parts/:id/status", handlers.UpdateStatus) - protected.DELETE("/parts/:id", handlers.DeletePart) + api.POST("/register", handlers.Register) + api.POST("/login", handlers.Login) + protected := api.Group("/") + protected.Use(middleware.AuthRequired()) + { + protected.GET("/parts/:id", handlers.GetPart) + protected.GET("/parts", handlers.GetAllParts) + protected.GET("/orders", handlers.GetsOrders) + protected.POST("/parts", handlers.CreatePart) + protected.POST("/parts/bulk", handlers.ImportParts) + protected.PATCH("/parts/:id/status", handlers.UpdateStatus) + protected.DELETE("/parts/:id", handlers.DeletePart) + + } } r.Run(":8090") diff --git a/frontend/src/components/AuthForm.jsx b/frontend/src/components/AuthForm.jsx index 67635d2..bb302c2 100644 --- a/frontend/src/components/AuthForm.jsx +++ b/frontend/src/components/AuthForm.jsx @@ -3,35 +3,26 @@ import axios from "axios"; import { useNavigate } from "react-router-dom"; const AuthForm = () => { - const [isLogin, setIsLogin] = useState(true); const [formData, setFormData] = useState({ username: "", password: "" }); const [message, setMessage] = useState({ text: "", isError: false }); const navigate = useNavigate(); - const API_BASE = ""; + const API_BASE = "/api"; const handleSubmit = async (e) => { e.preventDefault(); setMessage({ text: "", isError: false }); - const endpoint = isLogin ? "/login" : "/register"; - try { - const { data } = await axios.post(`${API_BASE}${endpoint}`, formData); + const { data } = await axios.post(`${API_BASE}/login`, formData); - if (isLogin) { + if (data.token) { localStorage.setItem("token", data.token); navigate("/"); - } else { - setMessage({ - text: "Регистрация успешна! Теперь войдите", - isError: false, - }); - setIsLogin(true); } } catch (err) { setMessage({ - text: err.response?.data?.error || "Ошибка сервера", + text: err.response?.data?.error || "Неверный логин или пароль", isError: true, }); } @@ -41,16 +32,17 @@ const AuthForm = () => {