summaryrefslogtreecommitdiff
path: root/src/components/header/authdialog/register.ts
diff options
context:
space:
mode:
authorl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-03-31 13:52:16 +0300
committerl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-03-31 13:52:16 +0300
commit37f51ee88710868a77b4645294cf32862f55e7c4 (patch)
treeac399bde1d35b6a52dd7421f68dd9567f7a71961 /src/components/header/authdialog/register.ts
parentcdc736b4823a3dda97f2b49c04b8ef7321e7a254 (diff)
add jwt
Diffstat (limited to 'src/components/header/authdialog/register.ts')
-rw-r--r--src/components/header/authdialog/register.ts76
1 files changed, 52 insertions, 24 deletions
diff --git a/src/components/header/authdialog/register.ts b/src/components/header/authdialog/register.ts
index bcbc21b..ba8bceb 100644
--- a/src/components/header/authdialog/register.ts
+++ b/src/components/header/authdialog/register.ts
@@ -1,44 +1,37 @@
+// register.ts
export const validate = (
username: string,
email: string,
password: string,
passwordConfirm: string,
) => {
- const newErrors: any = {};
+ const newErrors: Record<string, string> = {};
- if (!username) {
- newErrors.username = 'Введите никнейм';
- } else if (username.length < 3) {
- newErrors.username = 'Минимум 3 символа.';
- } else if (username.length > 32) {
- newErrors.username = 'Максимум 32 символа';
- }
+ if (!username) newErrors.username = 'Введите никнейм';
+ else if (username.length < 3) newErrors.username = 'Минимум 3 символа.';
+ else if (username.length > 32) newErrors.username = 'Максимум 32 символа';
- if (!email) {
- newErrors.email = 'Введите email.';
- } else {
+ if (!email) newErrors.email = 'Введите email.';
+ else {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) newErrors.email = 'Некорректный email';
if (email.length > 254) newErrors.email = 'Слишком длинный email.';
}
- if (!password) {
- newErrors.password = 'Введите пароль.';
- } else if (password.length < 8) {
+ if (!password) newErrors.password = 'Введите пароль.';
+ else if (password.length < 8)
newErrors.password = 'Необходимо минимум 8 символов.';
- } else {
+ else {
const hasLetter = /[A-Za-z]/.test(password);
const hasNumber = /\d/.test(password);
const hasSymbol = /[^\w\s]/.test(password);
-
if (!(hasLetter && hasNumber && hasSymbol)) {
newErrors.password = 'Попробуйте сочетание букв, цифр и символов.';
}
}
- if (password !== passwordConfirm) {
- newErrors.passwordConfirm = 'пароли не совпадают.';
- }
+ if (password !== passwordConfirm)
+ newErrors.passwordConfirm = 'Пароли не совпадают.';
return newErrors;
};
@@ -54,16 +47,51 @@ export const registerUser = async (
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, email, password }),
});
-
const data = await res.json();
if (!res.ok) {
- const { field, message } = data.detail;
- return { error: { [field]: message } };
+ const { field, message } = data.detail || {};
+ return {
+ data: null,
+ error: field
+ ? { [field]: message }
+ : { general: 'Ошибка регистрации' },
+ };
}
- return { data };
+ const body = new URLSearchParams();
+ body.append('username', username);
+ body.append('password', password);
+
+ const loginRes = await fetch('http://localhost:8000/api/auth/login', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
+ body: body.toString(),
+ });
+
+ const loginData = await loginRes.json();
+
+ if (!loginRes.ok) {
+ return {
+ data: null,
+ error: {
+ general:
+ loginData.detail || 'Ошибка входа после регистрации',
+ },
+ };
+ }
+
+ localStorage.setItem('token', loginData.access_token);
+ localStorage.setItem('refresh_token', loginData.refresh_token);
+
+ const meRes = await fetch('http://localhost:8000/api/me', {
+ headers: { Authorization: `Bearer ${loginData.access_token}` },
+ });
+
+ const meData = await meRes.json();
+
+ return { data: meData, error: null };
} catch (err: any) {
- return { error: { general: err.message } };
+ return { data: null, error: { general: err.message } };
}
};