Добавлено сохранение логина пользователя в куки при авторизации и отображение его в навигации. Обновлен компонент Navigation для отображения первых двух букв логина или имени по умолчанию.

This commit is contained in:
Redsandyg 2025-06-03 17:28:15 +03:00
parent af0c52dbb6
commit 0e024b00a1
2 changed files with 16 additions and 4 deletions

View File

@ -52,8 +52,9 @@ export default function AuthPage() {
return; return;
} }
const data = await res.json(); const data = await res.json();
// Сохраняем токен в куки на 60 минут через js-cookie // Сохраняем токен и логин в куки на 60 минут через js-cookie
Cookies.set('access_token', data.access_token, { expires: 1/24, path: '/', sameSite: 'strict'}); Cookies.set('access_token', data.access_token, { expires: 1/24, path: '/' });
Cookies.set('user_login', email, { expires: 1/24, path: '/' });
setError(""); setError("");
window.location.href = "/"; window.location.href = "/";
} catch (err) { } catch (err) {

View File

@ -2,6 +2,8 @@
import Link from "next/link"; import Link from "next/link";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import styles from "../styles/navigation.module.css"; import styles from "../styles/navigation.module.css";
import Cookies from "js-cookie";
import { useEffect, useState } from "react";
interface NavItem { interface NavItem {
id: string; id: string;
@ -17,6 +19,15 @@ const navItems: NavItem[] = [
const Navigation: React.FC = () => { const Navigation: React.FC = () => {
const pathname = usePathname(); const pathname = usePathname();
const [login, setLogin] = useState<string>("");
useEffect(() => {
if (typeof document !== "undefined") {
const userLogin = Cookies.get('user_login');
if (userLogin) setLogin(userLogin);
}
}, []);
if (pathname === "/auth") return null; if (pathname === "/auth") return null;
return ( return (
<nav className={styles.nav}> <nav className={styles.nav}>
@ -37,8 +48,8 @@ const Navigation: React.FC = () => {
))} ))}
</div> </div>
<div className={styles.profile}> <div className={styles.profile}>
<div className={styles.avatar}>ПП</div> <div className={styles.avatar}>{login ? login.slice(0, 2).toUpperCase() : "ПП"}</div>
<span className={styles.profileName}>Партнер RE:Premium</span> <span className={styles.profileName}>{login ? login : "Партнер RE:Premium"}</span>
</div> </div>
</nav> </nav>
); );