diff --git a/middleware.ts b/middleware.ts index cfac77a..01b22b3 100644 --- a/middleware.ts +++ b/middleware.ts @@ -5,6 +5,11 @@ export function middleware(request: NextRequest) { // Получаем access_token из куков (SSR) const token = request.cookies.get('access_token'); + // Если не на /auth и нет токена, редиректим на /auth + if (pathname !== '/auth' && !token) { + return NextResponse.redirect(new URL('/auth', request.url)); + } + // Если на /auth и токен есть, редиректим на главную if (pathname === '/auth' && token) { return NextResponse.redirect(new URL('/', request.url)); } @@ -12,5 +17,5 @@ export function middleware(request: NextRequest) { } export const config = { - matcher: ['/auth'], + matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'], }; \ No newline at end of file diff --git a/src/app/account/page.tsx b/src/app/account/page.tsx index d3d9507..b6ffacd 100644 --- a/src/app/account/page.tsx +++ b/src/app/account/page.tsx @@ -1,6 +1,7 @@ "use client"; import { useEffect, useState } from "react"; import styles from "../../styles/dashboard.module.css"; +import AuthGuard from "../../components/AuthGuard"; interface AccountData { id: number; @@ -11,40 +12,42 @@ interface AccountData { } export default function AccountPage() { - const [account, setAccount] = useState(null); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(null); +// const [account, setAccount] = useState(null); +// const [loading, setLoading] = useState(true); +// const [error, setError] = useState(null); - useEffect(() => { - fetch("/api/account") - .then((res) => { - if (!res.ok) throw new Error("Ошибка загрузки данных аккаунта"); - return res.json(); - }) - .then((data) => { - setAccount(data); - setLoading(false); - }) - .catch((err) => { - setError(err.message); - setLoading(false); - }); - }, []); +// useEffect(() => { +// fetch("/api/account") +// .then((res) => { +// if (!res.ok) throw new Error("Ошибка загрузки данных аккаунта"); +// return res.json(); +// }) +// .then((data) => { +// setAccount(data); +// setLoading(false); +// }) +// .catch((err) => { +// setError(err.message); +// setLoading(false); +// }); +// }, []); - if (loading) return
Загрузка...
; - if (error) return
Ошибка: {error}
; - if (!account) return
Нет данных
; +// if (loading) return
Загрузка...
; +// if (error) return
Ошибка: {error}
; +// if (!account) return
Нет данных
; return ( -
-

Аккаунт

-
-
ID: {account.id}
-
Логин: {account.login}
-
Имя: {account.name || "-"}
-
Email: {account.email || "-"}
-
Баланс: {account.balance.toLocaleString("ru-RU", { style: "currency", currency: "RUB" })}
+ +
+

Аккаунт

+
+ {/*
ID: {account.id}
+
Логин: {account.login}
+
Имя: {account.name || "-"}
+
Email: {account.email || "-"}
+
Баланс: {account.balance.toLocaleString("ru-RU", { style: "currency", currency: "RUB" })}
*/} +
-
+ ); } \ No newline at end of file diff --git a/src/app/auth/page.tsx b/src/app/auth/page.tsx index 3744a8c..71975de 100644 --- a/src/app/auth/page.tsx +++ b/src/app/auth/page.tsx @@ -14,6 +14,11 @@ export default function AuthPage() { const [error, setError] = useState(""); const [loading, setLoading] = useState(false); + useEffect(() => { + // Удаляем токен при заходе на страницу авторизации + Cookies.remove('access_token', { path: '/' }); + }, []); + useEffect(() => { if (hasToken()) { window.location.href = "/"; diff --git a/src/app/billing/page.tsx b/src/app/billing/page.tsx index dc97459..8f07bdf 100644 --- a/src/app/billing/page.tsx +++ b/src/app/billing/page.tsx @@ -7,6 +7,7 @@ import BillingMetricCards from "../../components/BillingMetricCards"; import PayoutsTransactionsTable from "../../components/PayoutsTransactionsTable"; import BillingStatChart from "../../components/BillingStatChart"; import DateFilters from "../../components/DateFilters"; +import AuthGuard from "../../components/AuthGuard"; export default function BillingPage() { const [payoutForm, setPayoutForm] = useState({ @@ -36,29 +37,31 @@ export default function BillingPage() { } return ( -
-

Финансы

- -
- - + +
+

Финансы

+ +
+ + +
+ { + if (field === "dateStart") { + if (filters.dateEnd && value > filters.dateEnd) return; + } + if (field === "dateEnd") { + if (filters.dateStart && value < filters.dateStart) return; + } + setFilters(f => ({ ...f, [field]: value })); + }} + onApply={handleApply} + onClear={handleClear} + /> +
- { - if (field === "dateStart") { - if (filters.dateEnd && value > filters.dateEnd) return; - } - if (field === "dateEnd") { - if (filters.dateStart && value < filters.dateStart) return; - } - setFilters(f => ({ ...f, [field]: value })); - }} - onApply={handleApply} - onClear={handleClear} - /> - -
+ ); } \ No newline at end of file diff --git a/src/app/page.tsx b/src/app/page.tsx index 94dffb1..d89e066 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,8 +1,8 @@ -import mockData from "../data/mockData"; +'use client' import MetricCards from "../components/MetricCards"; -import Table from "../components/Table"; import StatCharts from "../components/StatCharts"; import styles from "../styles/dashboard.module.css"; +import AuthGuard from "../components/AuthGuard"; function formatCurrency(amount: number) { return amount.toLocaleString("ru-RU", { @@ -14,31 +14,33 @@ function formatCurrency(amount: number) { export default function DashboardPage() { return ( -
-

Дашборд

- - - {/*
-

Последние продажи

- ( - - - - - - - - - )} - /> - */} - + +
+

Дашборд

+ + + {/*
+

Последние продажи

+
{sale.id}{sale.agent}{formatCurrency(sale.amount)}{formatCurrency(sale.commission)}{sale.date} - - {sale.status} - -
( + + + + + + + + + )} + /> + */} + + ); } diff --git a/src/app/stat/page.tsx b/src/app/stat/page.tsx index b6e7158..a50c5da 100644 --- a/src/app/stat/page.tsx +++ b/src/app/stat/page.tsx @@ -7,6 +7,7 @@ import SalesTable from "../../components/SalesTable"; import styles from "../../styles/stat.module.css"; import DateInput from "../../components/DateInput"; import DateFilters from "../../components/DateFilters"; +import AuthGuard from "../../components/AuthGuard"; const tabs = [ { id: "agents", label: "Агенты" }, @@ -32,53 +33,53 @@ export default function StatPage() { setReloadKey(k => k + 1); } - - return ( -
-
-

Статистика и аналитика

- {/* */} -
-
- {tabs.map((tab) => ( - - ))} -
- - { - if (field === "dateStart") { - if (filters.dateEnd && value > filters.dateEnd) return; - } - if (field === "dateEnd") { - if (filters.dateStart && value < filters.dateStart) return; - } - setFilters(f => ({ ...f, [field]: value })); - }} - onApply={handleApply} - onClear={handleClear} - /> + +
+
+

Статистика и аналитика

+ {/* */} +
+
+ {tabs.map((tab) => ( + + ))} +
+ + { + if (field === "dateStart") { + if (filters.dateEnd && value > filters.dateEnd) return; + } + if (field === "dateEnd") { + if (filters.dateStart && value < filters.dateStart) return; + } + setFilters(f => ({ ...f, [field]: value })); + }} + onApply={handleApply} + onClear={handleClear} + /> -
- {activeTab === "agents" && ( - - )} - {activeTab === "referrals" && ( - - )} - {activeTab === "sales" && ( - - )} +
+ {activeTab === "agents" && ( + + )} + {activeTab === "referrals" && ( + + )} + {activeTab === "sales" && ( + + )} +
-
+
); } \ No newline at end of file diff --git a/src/components/AuthGuard.tsx b/src/components/AuthGuard.tsx new file mode 100644 index 0000000..c3f704c --- /dev/null +++ b/src/components/AuthGuard.tsx @@ -0,0 +1,23 @@ +"use client"; +import { useEffect, useState, ReactNode } from "react"; +import Cookies from "js-cookie"; + +interface AuthGuardProps { + children: ReactNode; +} + +export default function AuthGuard({ children }: AuthGuardProps) { + const [checked, setChecked] = useState(false); + + useEffect(() => { + const token = Cookies.get('access_token'); + if (!token) { + window.location.href = "/auth"; + } else { + setChecked(true); + } + }, []); + + if (!checked) return null; + return <>{children}; +} \ No newline at end of file
{sale.id}{sale.agent}{formatCurrency(sale.amount)}{formatCurrency(sale.commission)}{sale.date} + + {sale.status} + +