Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c190dc874 | |||
| 36db09e5e7 | |||
| cf68530fc2 |
@@ -20,7 +20,7 @@ export const apiClient = axios.create({
|
|||||||
export const login = async (username, password) => {
|
export const login = async (username, password) => {
|
||||||
try {
|
try {
|
||||||
const response = await apiClient.post("/users/login", { username, password })
|
const response = await apiClient.post("/users/login", { username, password })
|
||||||
return response.data
|
return response
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ export const login = async (username, password) => {
|
|||||||
export const register = async (username, email, password, fname, lname, phone) => {
|
export const register = async (username, email, password, fname, lname, phone) => {
|
||||||
try {
|
try {
|
||||||
const response = await apiClient.post("/users/create", { username, email, password, fname, lname, phone })
|
const response = await apiClient.post("/users/create", { username, email, password, fname, lname, phone })
|
||||||
return { ...response.data, status: response.status }
|
return response
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import InputBox from "../../components/Inputs/InputBox"
|
|||||||
import Button from "../../components/Buttons/Button"
|
import Button from "../../components/Buttons/Button"
|
||||||
import { motion } from "framer-motion"
|
import { motion } from "framer-motion"
|
||||||
import { useState, useEffect } from "react"
|
import { useState, useEffect } from "react"
|
||||||
import { useLocation } from "react-router-dom"
|
import { useLocation, useNavigate } from "react-router-dom"
|
||||||
import { login } from "../../api/userApi"
|
import { login } from "../../api/userApi"
|
||||||
|
|
||||||
export default function LoginForm() {
|
export default function LoginForm() {
|
||||||
@@ -13,7 +13,9 @@ export default function LoginForm() {
|
|||||||
const [password, setPassword] = useState("")
|
const [password, setPassword] = useState("")
|
||||||
const [error, setError] = useState("")
|
const [error, setError] = useState("")
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
|
const navigate = useNavigate()
|
||||||
const [showSuccess, setShowSuccess] = useState(false)
|
const [showSuccess, setShowSuccess] = useState(false)
|
||||||
|
const [showErrorPopup, setShowErrorPopup] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (location.state && location.state.success) {
|
if (location.state && location.state.success) {
|
||||||
@@ -29,22 +31,40 @@ export default function LoginForm() {
|
|||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
setError("")
|
setError("")
|
||||||
|
setShowErrorPopup(false)
|
||||||
if (!email || !password) {
|
if (!email || !password) {
|
||||||
setError("Minden mező kitöltése kötelező.")
|
setError("Minden mező kitöltése kötelező.")
|
||||||
|
setShowErrorPopup(true)
|
||||||
|
setTimeout(() => setShowErrorPopup(false), 2000)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!validateEmail(email)) {
|
if (!validateEmail(email)) {
|
||||||
setError("Hibás email formátum.")
|
setError("Hibás email formátum.")
|
||||||
|
setShowErrorPopup(true)
|
||||||
|
setTimeout(() => setShowErrorPopup(false), 2000)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Backend API
|
// Backend API
|
||||||
login(email, password)
|
login(email, password)
|
||||||
.then((data) => {
|
.then((response) => {
|
||||||
console.log(data)
|
console.log(response)
|
||||||
console.log("Bejelentkezés:", { email, password })
|
// Csak a response.status-t ellenőrizd!
|
||||||
|
if (response && response.status === 200) {
|
||||||
|
if (response.data && response.data.user) {
|
||||||
|
localStorage.setItem("username", response.data.user.username)
|
||||||
|
localStorage.setItem("authLevel", response.data.user.authLevel)
|
||||||
|
}
|
||||||
|
navigate("/home")
|
||||||
|
} else {
|
||||||
|
setError("Hibás bejelentkezési adatok.")
|
||||||
|
setShowErrorPopup(true)
|
||||||
|
setTimeout(() => setShowErrorPopup(false), 2000)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
setError("Hibás bejelentkezési adatok.")
|
setError("Hibás bejelentkezési adatok.")
|
||||||
|
setShowErrorPopup(true)
|
||||||
|
setTimeout(() => setShowErrorPopup(false), 2000)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +82,11 @@ export default function LoginForm() {
|
|||||||
Sikeres regisztráció! Az email ellenőrzése után be tudsz lépni.
|
Sikeres regisztráció! Az email ellenőrzése után be tudsz lépni.
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{error && <div className="mb-4 text-red-600 text-center font-semibold">{error}</div>}
|
{showErrorPopup && error && (
|
||||||
|
<div className="fixed top-6 left-1/2 -translate-x-1/2 bg-red-500 text-white px-6 py-2 rounded shadow-lg z-50 text-center font-semibold transition-opacity duration-300">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
<InputBox
|
<InputBox
|
||||||
type="email"
|
type="email"
|
||||||
|
|||||||
@@ -51,12 +51,10 @@ export default function RegisterForm() {
|
|||||||
// Check for 201 Created status
|
// Check for 201 Created status
|
||||||
if (response && response.status === 201) {
|
if (response && response.status === 201) {
|
||||||
navigate("/login", { state: { success: true } })
|
navigate("/login", { state: { success: true } })
|
||||||
console.log(response)
|
|
||||||
console.log("Regisztráció:", { username, email, password, firstname, lastname, phone })
|
|
||||||
} else {
|
} else {
|
||||||
let msg = "Sikertelen regisztráció."
|
let msg = "Sikertelen regisztráció."
|
||||||
if (response && response.error) {
|
if (response && response.data && response.data.error) {
|
||||||
msg = response.error
|
msg = response.data.error
|
||||||
}
|
}
|
||||||
setError(msg)
|
setError(msg)
|
||||||
setShowErrorPopup(true)
|
setShowErrorPopup(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user