Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d348e0c347 | |||
| 444bc05f9d |
@@ -1,4 +1,5 @@
|
||||
import { Navigate } from "react-router-dom";
|
||||
import { authService } from "@/modules/auth/api/auth.service";
|
||||
|
||||
interface ProtectedRouteProps {
|
||||
children: React.ReactNode;
|
||||
@@ -9,7 +10,9 @@ export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
|
||||
children,
|
||||
fallbackPath = "/",
|
||||
}) => {
|
||||
if (false) {
|
||||
const isAuthenticated = authService.isAuthenticated();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to={fallbackPath} replace />;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Suspense } from "react";
|
||||
import { Routes as ReactRoutes, Route } from "react-router-dom";
|
||||
import { HomePage } from "@/pages/home.page";
|
||||
import { SecondaryPage } from "@/pages/secondary.page";
|
||||
import { AuthPage } from "@/pages/AuthPage";
|
||||
import { CreateOrganizationPage } from "@/pages/CreateOrganizationPage";
|
||||
import { OrganizationPage } from "@/pages/OrganizationPage";
|
||||
import { ProtectedRoute } from "./helper/protected.route";
|
||||
import { Routes as ReactRoutes, Route } from "react-router-dom";
|
||||
|
||||
export const Routing = () => {
|
||||
return (
|
||||
@@ -14,9 +17,27 @@ export const Routing = () => {
|
||||
}
|
||||
>
|
||||
<ReactRoutes>
|
||||
<Route path="/home" element={<HomePage />} />
|
||||
|
||||
<Route>
|
||||
<Route path="/" element={<AuthPage />} />
|
||||
<Route
|
||||
path="/create-organization"
|
||||
element={<CreateOrganizationPage />}
|
||||
/>
|
||||
<Route
|
||||
path="/home"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<HomePage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/organization"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<OrganizationPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/secondary"
|
||||
element={
|
||||
@@ -25,7 +46,6 @@ export const Routing = () => {
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
</Route>
|
||||
</ReactRoutes>
|
||||
</Suspense>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
import type {
|
||||
LoginCredentials,
|
||||
RegisterData,
|
||||
AuthResponse,
|
||||
OrganizationCreateData,
|
||||
OrganizationResponse,
|
||||
OrganizationMember,
|
||||
} from "../types/auth.types";
|
||||
|
||||
// Заглушка для хранения данных в localStorage
|
||||
const MOCK_USERS_KEY = "mock_users";
|
||||
const MOCK_ORGS_KEY = "mock_organizations";
|
||||
const MOCK_MEMBERS_KEY = "mock_members";
|
||||
|
||||
const getMockUsers = () => {
|
||||
const users = localStorage.getItem(MOCK_USERS_KEY);
|
||||
if (!users) {
|
||||
const defaultUsers = [
|
||||
{
|
||||
id: 1,
|
||||
username: "admin",
|
||||
email: "admin@example.com",
|
||||
password: "admin123",
|
||||
first_name: "Admin",
|
||||
last_name: "User",
|
||||
},
|
||||
];
|
||||
localStorage.setItem(MOCK_USERS_KEY, JSON.stringify(defaultUsers));
|
||||
return defaultUsers;
|
||||
}
|
||||
return JSON.parse(users);
|
||||
};
|
||||
|
||||
const getMockOrganizations = () => {
|
||||
const orgs = localStorage.getItem(MOCK_ORGS_KEY);
|
||||
if (!orgs) {
|
||||
return [];
|
||||
}
|
||||
return JSON.parse(orgs);
|
||||
};
|
||||
|
||||
const getMockMembers = () => {
|
||||
const members = localStorage.getItem(MOCK_MEMBERS_KEY);
|
||||
if (!members) {
|
||||
return [];
|
||||
}
|
||||
return JSON.parse(members);
|
||||
};
|
||||
|
||||
export const authService = {
|
||||
async login(credentials: LoginCredentials): Promise<AuthResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
const users = getMockUsers();
|
||||
const user = users.find(
|
||||
(u: any) =>
|
||||
u.username === credentials.username &&
|
||||
u.password === credentials.password,
|
||||
);
|
||||
|
||||
if (user) {
|
||||
resolve({
|
||||
access_token: `mock_token_${user.id}_${Date.now()}`,
|
||||
token_type: "bearer",
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
reject({
|
||||
response: {
|
||||
data: { message: "Неверное имя пользователя или пароль" },
|
||||
},
|
||||
});
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async register(
|
||||
data: Omit<RegisterData, "confirmPassword">,
|
||||
): Promise<AuthResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
const users = getMockUsers();
|
||||
const existingUser = users.find(
|
||||
(u: any) => u.username === data.username,
|
||||
);
|
||||
|
||||
if (existingUser) {
|
||||
reject({
|
||||
response: {
|
||||
data: { message: "Пользователь с таким именем уже существует" },
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const existingEmail = users.find((u: any) => u.email === data.email);
|
||||
if (existingEmail) {
|
||||
reject({
|
||||
response: {
|
||||
data: { message: "Пользователь с таким email уже существует" },
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const newUser = {
|
||||
id: users.length + 1,
|
||||
username: data.username,
|
||||
email: data.email,
|
||||
password: data.password,
|
||||
first_name: data.first_name,
|
||||
last_name: data.last_name,
|
||||
};
|
||||
|
||||
users.push(newUser);
|
||||
localStorage.setItem(MOCK_USERS_KEY, JSON.stringify(users));
|
||||
|
||||
resolve({
|
||||
access_token: `mock_token_${newUser.id}_${Date.now()}`,
|
||||
token_type: "bearer",
|
||||
user: {
|
||||
id: newUser.id,
|
||||
username: newUser.username,
|
||||
email: newUser.email,
|
||||
first_name: newUser.first_name,
|
||||
last_name: newUser.last_name,
|
||||
},
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async createOrganization(
|
||||
data: OrganizationCreateData,
|
||||
): Promise<OrganizationResponse> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
const orgs = getMockOrganizations();
|
||||
const token = this.getToken();
|
||||
const userId = token ? parseInt(token.split("_")[1]) : 1;
|
||||
|
||||
const newOrg = {
|
||||
id: orgs.length + 1,
|
||||
name: data.name,
|
||||
logo_url: data.logo ? URL.createObjectURL(data.logo) : undefined,
|
||||
created_at: new Date().toISOString(),
|
||||
owner_id: userId,
|
||||
};
|
||||
|
||||
orgs.push(newOrg);
|
||||
localStorage.setItem(MOCK_ORGS_KEY, JSON.stringify(orgs));
|
||||
|
||||
// Добавляем владельца в члены организации
|
||||
const members = getMockMembers();
|
||||
const newMember = {
|
||||
id: members.length + 1,
|
||||
organization_id: newOrg.id,
|
||||
user_id: userId,
|
||||
username: "temp_username",
|
||||
email: "temp_email",
|
||||
first_name: "temp_first",
|
||||
last_name: "temp_last",
|
||||
role: "owner",
|
||||
joined_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Получаем данные пользователя
|
||||
const users = getMockUsers();
|
||||
const user = users.find((u: any) => u.id === userId);
|
||||
if (user) {
|
||||
newMember.username = user.username;
|
||||
newMember.email = user.email;
|
||||
newMember.first_name = user.first_name;
|
||||
newMember.last_name = user.last_name;
|
||||
}
|
||||
|
||||
members.push(newMember);
|
||||
localStorage.setItem(MOCK_MEMBERS_KEY, JSON.stringify(members));
|
||||
|
||||
resolve(newOrg);
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async getOrganizations(): Promise<OrganizationResponse[]> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
const orgs = getMockOrganizations();
|
||||
resolve(orgs);
|
||||
}, 300);
|
||||
});
|
||||
},
|
||||
|
||||
async getOrganizationMembers(
|
||||
organizationId: number,
|
||||
): Promise<OrganizationMember[]> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
const members = getMockMembers();
|
||||
const orgMembers = members.filter(
|
||||
(m: any) => m.organization_id === organizationId,
|
||||
);
|
||||
resolve(orgMembers);
|
||||
}, 300);
|
||||
});
|
||||
},
|
||||
|
||||
async updateMemberRole(
|
||||
organizationId: number,
|
||||
userId: number,
|
||||
newRole: string,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
const members = getMockMembers();
|
||||
const memberIndex = members.findIndex(
|
||||
(m: any) =>
|
||||
m.organization_id === organizationId && m.user_id === userId,
|
||||
);
|
||||
|
||||
if (memberIndex !== -1) {
|
||||
members[memberIndex].role = newRole;
|
||||
localStorage.setItem(MOCK_MEMBERS_KEY, JSON.stringify(members));
|
||||
resolve();
|
||||
} else {
|
||||
reject({ response: { data: { message: "Участник не найден" } } });
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async removeMember(organizationId: number, userId: number): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
const members = getMockMembers();
|
||||
const filteredMembers = members.filter(
|
||||
(m: any) =>
|
||||
!(m.organization_id === organizationId && m.user_id === userId),
|
||||
);
|
||||
|
||||
if (filteredMembers.length !== members.length) {
|
||||
localStorage.setItem(
|
||||
MOCK_MEMBERS_KEY,
|
||||
JSON.stringify(filteredMembers),
|
||||
);
|
||||
resolve();
|
||||
} else {
|
||||
reject({ response: { data: { message: "Участник не найден" } } });
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async addMember(
|
||||
organizationId: number,
|
||||
email: string,
|
||||
role: string,
|
||||
): Promise<OrganizationMember> {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
const users = getMockUsers();
|
||||
const user = users.find((u: any) => u.email === email);
|
||||
|
||||
if (!user) {
|
||||
reject({
|
||||
response: {
|
||||
data: { message: "Пользователь с таким email не найден" },
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const members = getMockMembers();
|
||||
const existingMember = members.find(
|
||||
(m: any) =>
|
||||
m.organization_id === organizationId && m.user_id === user.id,
|
||||
);
|
||||
|
||||
if (existingMember) {
|
||||
reject({
|
||||
response: {
|
||||
data: {
|
||||
message: "Пользователь уже является участником организации",
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const newMember = {
|
||||
id: members.length + 1,
|
||||
organization_id: organizationId,
|
||||
user_id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
role: role,
|
||||
joined_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
members.push(newMember);
|
||||
localStorage.setItem(MOCK_MEMBERS_KEY, JSON.stringify(members));
|
||||
|
||||
resolve(newMember);
|
||||
}, 500);
|
||||
});
|
||||
},
|
||||
|
||||
async getCurrentUser(): Promise<AuthResponse["user"]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = this.getToken();
|
||||
if (!token) {
|
||||
reject({ response: { data: { message: "Не авторизован" } } });
|
||||
return;
|
||||
}
|
||||
|
||||
const userId = parseInt(token.split("_")[1]);
|
||||
const users = getMockUsers();
|
||||
const user = users.find((u: any) => u.id === userId);
|
||||
|
||||
if (user) {
|
||||
resolve({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
});
|
||||
} else {
|
||||
reject({ response: { data: { message: "Пользователь не найден" } } });
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
logout(): void {
|
||||
localStorage.removeItem("auth-storage");
|
||||
localStorage.removeItem("organization-storage");
|
||||
},
|
||||
|
||||
saveAuthData(token: string, user: AuthResponse["user"]): void {
|
||||
const authStorage = {
|
||||
state: {
|
||||
token,
|
||||
user,
|
||||
},
|
||||
version: 0,
|
||||
};
|
||||
localStorage.setItem("auth-storage", JSON.stringify(authStorage));
|
||||
},
|
||||
|
||||
saveOrganization(organization: OrganizationResponse): void {
|
||||
localStorage.setItem("organization-storage", JSON.stringify(organization));
|
||||
},
|
||||
|
||||
getToken(): string | null {
|
||||
const authStorage = localStorage.getItem("auth-storage");
|
||||
if (!authStorage) return null;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(authStorage);
|
||||
return parsed.state?.token || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
getCurrentOrganization(): OrganizationResponse | null {
|
||||
const orgStorage = localStorage.getItem("organization-storage");
|
||||
if (!orgStorage) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(orgStorage);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
const token = this.getToken();
|
||||
return !!token;
|
||||
},
|
||||
|
||||
hasOrganization(): boolean {
|
||||
return !!this.getCurrentOrganization();
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,138 @@
|
||||
import React, { useState, useRef } from "react";
|
||||
import { useAuth } from "../hooks/useAuth";
|
||||
|
||||
export const CreateOrganizationForm = () => {
|
||||
const [organizationName, setOrganizationName] = useState("");
|
||||
const [logo, setLogo] = useState<File | null>(null);
|
||||
const [logoPreview, setLogoPreview] = useState<string>("");
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const { createOrganization, isLoading, error } = useAuth();
|
||||
|
||||
const handleLogoChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
setLogo(file);
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => {
|
||||
setLogoPreview(reader.result as string);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (organizationName.trim()) {
|
||||
await createOrganization({
|
||||
name: organizationName,
|
||||
logo: logo || undefined,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
<div className="flex justify-center">
|
||||
<div className="relative">
|
||||
<div
|
||||
className="w-32 h-32 rounded-full border-2 border-dashed flex items-center justify-center cursor-pointer overflow-hidden transition-all hover:border-accent"
|
||||
style={{
|
||||
borderColor: "var(--border)",
|
||||
backgroundColor: "var(--bg-secondary)",
|
||||
}}
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
>
|
||||
{logoPreview ? (
|
||||
<img
|
||||
src={logoPreview}
|
||||
alt="Organization logo"
|
||||
className="w-full h-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<div className="text-center">
|
||||
<svg
|
||||
className="w-10 h-10 mx-auto mb-1"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
style={{ color: "var(--text-muted)" }}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
|
||||
/>
|
||||
</svg>
|
||||
<span
|
||||
className="text-xs"
|
||||
style={{ color: "var(--text-muted)" }}
|
||||
>
|
||||
Загрузить лого
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={handleLogoChange}
|
||||
className="hidden"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="organizationName"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Название организации
|
||||
</label>
|
||||
<input
|
||||
id="organizationName"
|
||||
type="text"
|
||||
value={organizationName}
|
||||
onChange={(e) => setOrganizationName(e.target.value)}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Введите название организации"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div
|
||||
className="p-3 rounded-lg text-sm"
|
||||
style={{
|
||||
backgroundColor: "var(--error-bg)",
|
||||
borderColor: "var(--error-border)",
|
||||
color: "var(--error-text)",
|
||||
}}
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="w-full py-2 px-4 rounded-lg font-medium transition-all transform hover:scale-[1.02] active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
style={{
|
||||
backgroundColor: "var(--button-primary)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
{isLoading ? "Создание..." : "Создать организацию"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,109 @@
|
||||
import React, { useState } from "react";
|
||||
import { useAuth } from "../hooks/useAuth";
|
||||
|
||||
interface LoginFormProps {
|
||||
onSwitchToRegister: () => void;
|
||||
}
|
||||
|
||||
export const LoginForm: React.FC<LoginFormProps> = ({ onSwitchToRegister }) => {
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const { login, isLoading, error } = useAuth();
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
await login({ username, password });
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="username"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Имя пользователя
|
||||
</label>
|
||||
<input
|
||||
id="username"
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Введите имя пользователя"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Пароль
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Введите пароль"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div
|
||||
className="p-3 rounded-lg text-sm"
|
||||
style={{
|
||||
backgroundColor: "var(--error-bg)",
|
||||
borderColor: "var(--error-border)",
|
||||
color: "var(--error-text)",
|
||||
}}
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="w-full py-2 px-4 rounded-lg font-medium transition-all transform hover:scale-[1.02] active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
style={{
|
||||
backgroundColor: "var(--button-primary)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
{isLoading ? "Вход..." : "Войти"}
|
||||
</button>
|
||||
|
||||
<div className="text-center">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSwitchToRegister}
|
||||
className="text-sm transition-colors hover:underline"
|
||||
style={{ color: "var(--link)" }}
|
||||
>
|
||||
Нет аккаунта? Зарегистрироваться
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,231 @@
|
||||
import React, { useState } from "react";
|
||||
import { useAuth } from "../hooks/useAuth";
|
||||
|
||||
interface RegisterFormProps {
|
||||
onSwitchToLogin: () => void;
|
||||
}
|
||||
|
||||
export const RegisterForm: React.FC<RegisterFormProps> = ({
|
||||
onSwitchToLogin,
|
||||
}) => {
|
||||
const [formData, setFormData] = useState({
|
||||
first_name: "",
|
||||
last_name: "",
|
||||
username: "",
|
||||
email: "",
|
||||
password: "",
|
||||
confirmPassword: "",
|
||||
});
|
||||
|
||||
const { register, isLoading, error } = useAuth();
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setFormData({
|
||||
...formData,
|
||||
[e.target.id]: e.target.value,
|
||||
});
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
await register(formData);
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="first_name"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Имя
|
||||
</label>
|
||||
<input
|
||||
id="first_name"
|
||||
type="text"
|
||||
value={formData.first_name}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Имя"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="last_name"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Фамилия
|
||||
</label>
|
||||
<input
|
||||
id="last_name"
|
||||
type="text"
|
||||
value={formData.last_name}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Фамилия"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="username"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Имя пользователя
|
||||
</label>
|
||||
<input
|
||||
id="username"
|
||||
type="text"
|
||||
value={formData.username}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="username"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="email"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Email
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="example@mail.com"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Пароль
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
value={formData.password}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Введите пароль"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="confirmPassword"
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Подтверждение пароля
|
||||
</label>
|
||||
<input
|
||||
id="confirmPassword"
|
||||
type="password"
|
||||
value={formData.confirmPassword}
|
||||
onChange={handleChange}
|
||||
className="w-full px-4 py-2 rounded-lg border transition-all focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
disabled={isLoading}
|
||||
placeholder="Повторите пароль"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div
|
||||
className="p-3 rounded-lg text-sm"
|
||||
style={{
|
||||
backgroundColor: "var(--error-bg)",
|
||||
borderColor: "var(--error-border)",
|
||||
color: "var(--error-text)",
|
||||
}}
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="w-full py-2 px-4 rounded-lg font-medium transition-all transform hover:scale-[1.02] active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
style={{
|
||||
backgroundColor: "var(--button-primary)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
{isLoading ? "Регистрация..." : "Зарегистрироваться"}
|
||||
</button>
|
||||
|
||||
<div className="text-center">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSwitchToLogin}
|
||||
className="text-sm transition-colors hover:underline"
|
||||
style={{ color: "var(--link)" }}
|
||||
>
|
||||
Уже есть аккаунт? Войти
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,82 @@
|
||||
import { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { authService } from "../api/auth.service";
|
||||
import { useApi } from "@/shared/api/hooks/use.api";
|
||||
import type {
|
||||
LoginCredentials,
|
||||
RegisterData,
|
||||
OrganizationCreateData,
|
||||
OrganizationMember,
|
||||
} from "../types/auth.types";
|
||||
|
||||
export const useAuth = () => {
|
||||
const navigate = useNavigate();
|
||||
const { isLoading, error, request } = useApi();
|
||||
const [authError, setAuthError] = useState<string | null>(null);
|
||||
|
||||
const login = async (credentials: LoginCredentials) => {
|
||||
setAuthError(null);
|
||||
const result = await request(() => authService.login(credentials));
|
||||
|
||||
if (result) {
|
||||
authService.saveAuthData(result.access_token, result.user);
|
||||
|
||||
const orgs = await request(() => authService.getOrganizations());
|
||||
if (orgs && orgs.length > 0) {
|
||||
authService.saveOrganization(orgs[0]);
|
||||
navigate("/home");
|
||||
} else {
|
||||
navigate("/create-organization");
|
||||
}
|
||||
} else if (error) {
|
||||
setAuthError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const register = async (data: RegisterData) => {
|
||||
setAuthError(null);
|
||||
|
||||
if (data.password !== data.confirmPassword) {
|
||||
setAuthError("Пароли не совпадают");
|
||||
return;
|
||||
}
|
||||
|
||||
const { confirmPassword, ...registerData } = data;
|
||||
const result = await request(() => authService.register(registerData));
|
||||
|
||||
if (result) {
|
||||
authService.saveAuthData(result.access_token, result.user);
|
||||
navigate("/create-organization");
|
||||
} else if (error) {
|
||||
setAuthError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const createOrganization = async (data: OrganizationCreateData) => {
|
||||
setAuthError(null);
|
||||
const result = await request(() => authService.createOrganization(data));
|
||||
|
||||
if (result) {
|
||||
authService.saveOrganization(result);
|
||||
navigate("/home");
|
||||
} else if (error) {
|
||||
setAuthError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
authService.logout();
|
||||
navigate("/");
|
||||
};
|
||||
|
||||
return {
|
||||
login,
|
||||
register,
|
||||
createOrganization,
|
||||
logout,
|
||||
isLoading,
|
||||
error: authError,
|
||||
isAuthenticated: authService.isAuthenticated(),
|
||||
hasOrganization: authService.hasOrganization(),
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
export interface LoginCredentials {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface RegisterData {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
confirmPassword: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
user: {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OrganizationCreateData {
|
||||
name: string;
|
||||
logo?: File;
|
||||
}
|
||||
|
||||
export interface OrganizationResponse {
|
||||
id: number;
|
||||
name: string;
|
||||
logo_url?: string;
|
||||
created_at: string;
|
||||
owner_id: number;
|
||||
}
|
||||
|
||||
export interface OrganizationMember {
|
||||
id: number;
|
||||
user_id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
role: "owner" | "admin" | "member";
|
||||
joined_at: string;
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
import React, { useState } from "react";
|
||||
import { useOrganization } from "../hooks/useOrganization";
|
||||
|
||||
export const OrganizationMembers = () => {
|
||||
const { members, isLoading, updateMemberRole, removeMember, addMember } =
|
||||
useOrganization();
|
||||
const [showAddModal, setShowAddModal] = useState(false);
|
||||
const [newMemberEmail, setNewMemberEmail] = useState("");
|
||||
const [newMemberRole, setNewMemberRole] = useState<"admin" | "member">(
|
||||
"member",
|
||||
);
|
||||
const [actionLoading, setActionLoading] = useState<number | null>(null);
|
||||
|
||||
const handleRoleChange = async (userId: number, newRole: string) => {
|
||||
setActionLoading(userId);
|
||||
await updateMemberRole(userId, newRole);
|
||||
setActionLoading(null);
|
||||
};
|
||||
|
||||
const handleRemoveMember = async (userId: number, memberName: string) => {
|
||||
if (
|
||||
confirm(
|
||||
`Вы уверены, что хотите удалить пользователя "${memberName}" из организации?`,
|
||||
)
|
||||
) {
|
||||
setActionLoading(userId);
|
||||
await removeMember(userId);
|
||||
setActionLoading(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddMember = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const success = await addMember(newMemberEmail, newMemberRole);
|
||||
if (success) {
|
||||
setShowAddModal(false);
|
||||
setNewMemberEmail("");
|
||||
setNewMemberRole("member");
|
||||
}
|
||||
};
|
||||
|
||||
const getRoleLabel = (role: string) => {
|
||||
switch (role) {
|
||||
case "owner":
|
||||
return "Владелец";
|
||||
case "admin":
|
||||
return "Администратор";
|
||||
case "member":
|
||||
return "Участник";
|
||||
default:
|
||||
return role;
|
||||
}
|
||||
};
|
||||
|
||||
const getRoleColor = (role: string) => {
|
||||
switch (role) {
|
||||
case "owner":
|
||||
return "text-yellow-600 dark:text-yellow-400";
|
||||
case "admin":
|
||||
return "text-blue-600 dark:text-blue-400";
|
||||
case "member":
|
||||
return "text-green-600 dark:text-green-400";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading && members.length === 0) {
|
||||
return (
|
||||
<div className="flex justify-center py-12">
|
||||
<div style={{ color: "var(--text-secondary)" }}>
|
||||
Загрузка участников...
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h2
|
||||
className="text-2xl font-bold"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
Участники организации
|
||||
</h2>
|
||||
<button
|
||||
onClick={() => setShowAddModal(true)}
|
||||
className="px-4 py-2 rounded-lg font-medium transition-all transform hover:scale-[1.02] active:scale-[0.98]"
|
||||
style={{
|
||||
backgroundColor: "var(--accent)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
+ Добавить участника
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b" style={{ borderColor: "var(--border)" }}>
|
||||
<th
|
||||
className="text-left py-3 px-4"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Имя
|
||||
</th>
|
||||
<th
|
||||
className="text-left py-3 px-4"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Email
|
||||
</th>
|
||||
<th
|
||||
className="text-left py-3 px-4"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Роль
|
||||
</th>
|
||||
<th
|
||||
className="text-left py-3 px-4"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Действия
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{members.map((member) => (
|
||||
<tr
|
||||
key={member.id}
|
||||
className="border-b"
|
||||
style={{ borderColor: "var(--border)" }}
|
||||
>
|
||||
<td
|
||||
className="py-3 px-4"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
{member.first_name} {member.last_name}
|
||||
<div
|
||||
className="text-xs"
|
||||
style={{ color: "var(--text-muted)" }}
|
||||
>
|
||||
@{member.username}
|
||||
</div>
|
||||
</td>
|
||||
<td
|
||||
className="py-3 px-4"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
{member.email}
|
||||
</td>
|
||||
<td className="py-3 px-4">
|
||||
{member.role === "owner" ? (
|
||||
<span
|
||||
className={`font-medium ${getRoleColor(member.role)}`}
|
||||
>
|
||||
{getRoleLabel(member.role)}
|
||||
</span>
|
||||
) : (
|
||||
<select
|
||||
value={member.role}
|
||||
onChange={(e) =>
|
||||
handleRoleChange(member.user_id, e.target.value)
|
||||
}
|
||||
disabled={actionLoading === member.user_id}
|
||||
className="px-2 py-1 rounded border focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
>
|
||||
<option value="admin">{getRoleLabel("admin")}</option>
|
||||
<option value="member">{getRoleLabel("member")}</option>
|
||||
</select>
|
||||
)}
|
||||
</td>
|
||||
<td className="py-3 px-4">
|
||||
{member.role !== "owner" && (
|
||||
<button
|
||||
onClick={() =>
|
||||
handleRemoveMember(
|
||||
member.user_id,
|
||||
`${member.first_name} ${member.last_name}`,
|
||||
)
|
||||
}
|
||||
disabled={actionLoading === member.user_id}
|
||||
className="px-3 py-1 rounded text-sm transition-all hover:scale-105 disabled:opacity-50"
|
||||
style={{
|
||||
backgroundColor: "var(--button-danger)",
|
||||
color: "var(--button-danger-text)",
|
||||
}}
|
||||
>
|
||||
{actionLoading === member.user_id ? "..." : "Удалить"}
|
||||
</button>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{members.length === 0 && !isLoading && (
|
||||
<div
|
||||
className="text-center py-12"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
В организации пока нет участников
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showAddModal && (
|
||||
<div className="fixed inset-0 bg-[#00000055] bg-opacity-50 flex items-center justify-center z-50">
|
||||
<div
|
||||
className="rounded-2xl p-6 max-w-md w-full mx-4"
|
||||
style={{
|
||||
backgroundColor: "var(--card-bg)",
|
||||
boxShadow: `0 20px 25px -5px var(--shadow-color)`,
|
||||
}}
|
||||
>
|
||||
<h3
|
||||
className="text-xl font-bold mb-4"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
Добавить участника
|
||||
</h3>
|
||||
<form onSubmit={handleAddMember}>
|
||||
<div className="mb-4">
|
||||
<label
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Email пользователя
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
value={newMemberEmail}
|
||||
onChange={(e) => setNewMemberEmail(e.target.value)}
|
||||
className="w-full px-4 py-2 rounded-lg border focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
required
|
||||
placeholder="user@example.com"
|
||||
/>
|
||||
</div>
|
||||
<div className="mb-6">
|
||||
<label
|
||||
className="block text-sm font-medium mb-2"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Роль
|
||||
</label>
|
||||
<select
|
||||
value={newMemberRole}
|
||||
onChange={(e) =>
|
||||
setNewMemberRole(e.target.value as "admin" | "member")
|
||||
}
|
||||
className="w-full px-4 py-2 rounded-lg border focus:outline-none focus:ring-2"
|
||||
style={{
|
||||
backgroundColor: "var(--input-bg)",
|
||||
color: "var(--text-primary)",
|
||||
borderColor: "var(--border)",
|
||||
"--tw-ring-color": "var(--accent)",
|
||||
}}
|
||||
>
|
||||
<option value="admin">Администратор</option>
|
||||
<option value="member">Участник</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAddModal(false)}
|
||||
className="flex-1 py-2 px-4 rounded-lg font-medium transition-all"
|
||||
style={{
|
||||
backgroundColor: "var(--bg-secondary)",
|
||||
color: "var(--text-secondary)",
|
||||
}}
|
||||
>
|
||||
Отмена
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
className="flex-1 py-2 px-4 rounded-lg font-medium transition-all transform hover:scale-[1.02]"
|
||||
style={{
|
||||
backgroundColor: "var(--accent)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
Добавить
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,82 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { authService } from "@/modules/auth/api/auth.service";
|
||||
import { useApi } from "@/shared/api/hooks/use.api";
|
||||
import type { OrganizationMember } from "@/modules/auth/types/auth.types";
|
||||
|
||||
export const useOrganization = () => {
|
||||
const { isLoading, error, request } = useApi();
|
||||
const [members, setMembers] = useState<OrganizationMember[]>([]);
|
||||
const [organization, setOrganization] = useState(
|
||||
authService.getCurrentOrganization(),
|
||||
);
|
||||
|
||||
const loadMembers = async () => {
|
||||
if (organization) {
|
||||
const result = await request(() =>
|
||||
authService.getOrganizationMembers(organization.id),
|
||||
);
|
||||
if (result) {
|
||||
setMembers(result);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadMembers();
|
||||
}, [organization]);
|
||||
|
||||
const updateMemberRole = async (userId: number, newRole: string) => {
|
||||
if (organization) {
|
||||
const result = await request(() =>
|
||||
authService.updateMemberRole(organization.id, userId, newRole),
|
||||
);
|
||||
if (result !== undefined) {
|
||||
await loadMembers();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const removeMember = async (userId: number) => {
|
||||
if (organization) {
|
||||
const result = await request(() =>
|
||||
authService.removeMember(organization.id, userId),
|
||||
);
|
||||
if (result !== undefined) {
|
||||
await loadMembers();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const addMember = async (email: string, role: string) => {
|
||||
if (organization) {
|
||||
const result = await request(() =>
|
||||
authService.addMember(organization.id, email, role),
|
||||
);
|
||||
if (result) {
|
||||
await loadMembers();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const refreshOrganization = () => {
|
||||
setOrganization(authService.getCurrentOrganization());
|
||||
};
|
||||
|
||||
return {
|
||||
members,
|
||||
organization,
|
||||
isLoading,
|
||||
error,
|
||||
updateMemberRole,
|
||||
removeMember,
|
||||
addMember,
|
||||
refreshOrganization,
|
||||
loadMembers,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
import { useState } from "react";
|
||||
import { ThemeToggle } from "@/modules/theme-changer/ui/Theme.toggle";
|
||||
import { LoginForm } from "@/modules/auth/components/LoginForm";
|
||||
import { RegisterForm } from "@/modules/auth/components/RegisterForm";
|
||||
|
||||
export const AuthPage = () => {
|
||||
const [isLogin, setIsLogin] = useState(true);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="min-h-screen flex items-center justify-center p-4 transition-theme"
|
||||
style={{ backgroundColor: "var(--bg-primary)" }}
|
||||
>
|
||||
<div className="absolute top-4 right-4">
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="w-full max-w-md rounded-2xl shadow-xl p-8 transition-theme"
|
||||
style={{
|
||||
backgroundColor: "var(--card-bg)",
|
||||
boxShadow: `0 20px 25px -5px var(--shadow-color), 0 8px 10px -6px var(--shadow-color)`,
|
||||
}}
|
||||
>
|
||||
<div className="text-center mb-8">
|
||||
<h1
|
||||
className="text-3xl font-bold mb-2 transition-theme"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
IPS Manager
|
||||
</h1>
|
||||
<p
|
||||
className="text-sm transition-theme"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
{isLogin
|
||||
? "Войдите в систему для управления IPS агентами"
|
||||
: "Создайте аккаунт для начала работы"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{isLogin ? (
|
||||
<LoginForm onSwitchToRegister={() => setIsLogin(false)} />
|
||||
) : (
|
||||
<RegisterForm onSwitchToLogin={() => setIsLogin(true)} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
import { ThemeToggle } from "@/modules/theme-changer/ui/Theme.toggle";
|
||||
import { CreateOrganizationForm } from "@/modules/auth/components/CreateOrganizationForm";
|
||||
import { useAuth } from "@/modules/auth/hooks/useAuth";
|
||||
import { Navigate } from "react-router-dom";
|
||||
|
||||
export const CreateOrganizationPage = () => {
|
||||
const { isAuthenticated, hasOrganization } = useAuth();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
if (hasOrganization) {
|
||||
return <Navigate to="/home" replace />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className="min-h-screen flex items-center justify-center p-4 transition-theme"
|
||||
style={{ backgroundColor: "var(--bg-primary)" }}
|
||||
>
|
||||
<div className="absolute top-4 right-4">
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="w-full max-w-md rounded-2xl shadow-xl p-8 transition-theme"
|
||||
style={{
|
||||
backgroundColor: "var(--card-bg)",
|
||||
boxShadow: `0 20px 25px -5px var(--shadow-color), 0 8px 10px -6px var(--shadow-color)`,
|
||||
}}
|
||||
>
|
||||
<div className="text-center mb-8">
|
||||
<h1
|
||||
className="text-2xl font-bold mb-2 transition-theme"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
Создание организации
|
||||
</h1>
|
||||
<p
|
||||
className="text-sm transition-theme"
|
||||
style={{ color: "var(--text-secondary)" }}
|
||||
>
|
||||
Создайте организацию для начала работы с IPS платформой
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<CreateOrganizationForm />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,74 @@
|
||||
import React from "react";
|
||||
import { ThemeToggle } from "@/modules/theme-changer/ui/Theme.toggle";
|
||||
import { OrganizationMembers } from "@/modules/organization/components/OrganizationMembers";
|
||||
import { useAuth } from "@/modules/auth/hooks/useAuth";
|
||||
import { Navigate, useNavigate } from "react-router-dom";
|
||||
import { authService } from "@/modules/auth/api/auth.service";
|
||||
|
||||
export const OrganizationPage = () => {
|
||||
const { isAuthenticated, hasOrganization } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const organization = authService.getCurrentOrganization();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
if (!hasOrganization) {
|
||||
return <Navigate to="/create-organization" replace />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className="min-h-screen transition-theme"
|
||||
style={{ backgroundColor: "var(--bg-primary)" }}
|
||||
>
|
||||
<div className="absolute top-4 right-4">
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
<div className="mb-8">
|
||||
<button
|
||||
onClick={() => navigate("/home")}
|
||||
className="mb-4 inline-flex items-center gap-2 text-sm transition-colors hover:underline"
|
||||
style={{ color: "var(--link)" }}
|
||||
>
|
||||
← Назад
|
||||
</button>
|
||||
|
||||
<div className="flex items-center gap-4">
|
||||
{organization?.logo_url && (
|
||||
<img
|
||||
src={organization.logo_url}
|
||||
alt={organization.name}
|
||||
className="w-16 h-16 rounded-full object-cover"
|
||||
/>
|
||||
)}
|
||||
<div>
|
||||
<h1
|
||||
className="text-3xl font-bold"
|
||||
style={{ color: "var(--text-primary)" }}
|
||||
>
|
||||
{organization?.name}
|
||||
</h1>
|
||||
<p className="text-sm" style={{ color: "var(--text-secondary)" }}>
|
||||
Управление участниками организации
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="rounded-2xl p-6"
|
||||
style={{
|
||||
backgroundColor: "var(--card-bg)",
|
||||
boxShadow: `0 4px 6px -1px var(--shadow-color)`,
|
||||
}}
|
||||
>
|
||||
<OrganizationMembers />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
+36
-2
@@ -1,13 +1,47 @@
|
||||
import { ThemeToggle } from "@/modules/theme-changer/ui/Theme.toggle";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "@/modules/auth/hooks/useAuth";
|
||||
|
||||
export const HomePage = () => {
|
||||
const navigate = useNavigate();
|
||||
const { logout } = useAuth();
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{ backgroundColor: "var(--bg-primary)" }}
|
||||
className="min-h-screen flex items-center justify-center p-4"
|
||||
className="min-h-screen p-4"
|
||||
>
|
||||
<div className="flex justify-between items-center mb-8">
|
||||
<ThemeToggle />
|
||||
Домашняя
|
||||
<div className="flex gap-4">
|
||||
<button
|
||||
onClick={() => navigate("/organization")}
|
||||
className="px-4 py-2 rounded-lg font-medium transition-all"
|
||||
style={{
|
||||
backgroundColor: "var(--accent)",
|
||||
color: "var(--button-primary-text)",
|
||||
}}
|
||||
>
|
||||
Управление организацией
|
||||
</button>
|
||||
<button
|
||||
onClick={logout}
|
||||
className="px-4 py-2 rounded-lg font-medium transition-all"
|
||||
style={{
|
||||
backgroundColor: "var(--button-danger)",
|
||||
color: "var(--button-danger-text)",
|
||||
}}
|
||||
>
|
||||
Выйти
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-center">
|
||||
<h1 className="text-2xl" style={{ color: "var(--text-primary)" }}>
|
||||
Добро пожаловать в IPS Manager
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user