added registration

This commit is contained in:
SlimusMinus
2026-05-04 00:48:50 +03:00
parent cc4fcfd3dc
commit aee1213ff9
8 changed files with 742 additions and 172 deletions

69
src/hooks/useRegister.js Normal file
View File

@@ -0,0 +1,69 @@
import { useState, useEffect } from "react";
import {API_URL} from "../config/constants";
export function useCheckLogin(login) {
const [checking, setChecking] = useState(false);
const [exists, setExists] = useState(false);
useEffect(() => {
if (!login || login.length < 3) {
setExists(false);
return;
}
const timer = setTimeout(async () => {
try {
setChecking(true);
const res = await fetch(`${API_URL}/api/v1/auth/check-login?login=${login}`);
const json = await res.json();
setExists(json.exists);
} catch (e) {
console.error(e);
} finally {
setChecking(false);
}
}, 400);
return () => clearTimeout(timer);
}, [login]);
return { checking, exists };
}
export function useRegister() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [success, setSuccess] = useState(false);
const register = async ({ name, login, password }) => {
setLoading(true);
setError(null);
try {
const res = await fetch(`${API_URL}/api/v1/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, login, password }),
});
const data = await res.json();
if (!res.ok) {
throw new Error(data.message || "Ошибка регистрации");
}
setSuccess(true);
setTimeout(() => {
window.location.href = "/register";
}, 2500);
} catch (e) {
setError(e.message);
} finally {
setLoading(false);
}
};
return { register, loading, error, success };
}